Download notebook (.ipynb)

Expanding Plot Limits with expand_limits()#

When creating visualizations, you might occasionally need to adjust your plot boundaries to encompass specific data points or values. This is where the expand_limits() function comes in handy. It allows you to extend the plot’s scales to include particular values, ensuring they’re visible in your visualization.

import pandas as pd

from lets_plot import *
LetsPlot.setup_html()
df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg2.csv")
print(df.shape)
df.head()
(392, 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
0 18.0 8 307.0 130 3504 12.0 70 US chevrolet chevelle malibu
1 15.0 8 350.0 165 3693 11.5 70 US buick skylark 320
2 18.0 8 318.0 150 3436 11.0 70 US plymouth satellite
3 16.0 8 304.0 150 3433 12.0 70 US amc rebel sst
4 17.0 8 302.0 140 3449 10.5 70 US ford torino
p1 = ggplot(df, aes("miles per gallon", "vehicle weight (lbs.)")) + geom_point()   
p1

Expand Lower Limit Along the x-axis#

p1 + expand_limits(x=0)

Expand Limits Along the y-axis#

p1 + expand_limits(y=[1000, 9000])

Expand Lower Limits Along Both Axes#

p1 + expand_limits(x=0, y=0)

Expanding Color-scale Limits#

# Add color mapping
p2 = p1 + aes(color="number of cylinders")

gggrid([
    p2,
    # Expand the color-scale limits.
    p2 + expand_limits(color=range(2, 11, 2))
])