When creating visualizations, you might occasionally need to adjust your plot boundaries to encompass specific data points or values. This is where the expandLimits()
function comes in handy. It allows you to extend the plot's scales to include particular values, ensuring they're visible in your visualization.
%useLatestDescriptors
%use dataframe
%use lets-plot
LetsPlot.getInfo()
Lets-Plot Kotlin API v.4.9.3. Frontend: Notebook with dynamically loaded JS. Lets-Plot JS v.4.5.2.
val mpg = DataFrame.readCSV("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg2.csv")
mpg.head(3)
DataFrame: rowsCount = 3, columnsCount = 9
miles per gallon | number of cylinders | engine displacement (cu. inches) | engine horsepower | vehicle weight (lbs.) | time to accelerate (sec.) | model year | origin of car | vehicle name |
---|---|---|---|---|---|---|---|---|
18.000000 | 8 | 307.000000 | 130 | 3504 | 12.000000 | 70 | US | chevrolet chevelle malibu |
15.000000 | 8 | 350.000000 | 165 | 3693 | 11.500000 | 70 | US | buick skylark 320 |
18.000000 | 8 | 318.000000 | 150 | 3436 | 11.000000 | 70 | US | plymouth satellite |
val p = letsPlot(mpg.toMap()) { x = "miles per gallon"; y = "vehicle weight (lbs.)"} + geomPoint()
p
p + expandLimits(x = 0)
p + expandLimits(y = listOf(1000, 9000))
p + expandLimits(x = 0, y = 0)
// Same plot with color mapping
val p1 = letsPlot(mpg.toMap()) {
x = "miles per gallon"
y = "vehicle weight (lbs.)"
color = "number of cylinders"
} + geomPoint()
gggrid(listOf(
p1,
// Expand the color-scale limits.
p1 + expandLimits(color = 2..10)
))