Plotnine在Jupyter Lab绘图时边缘显示所有x、y值的问题求助
Yep, this is a known quirk with plotnine that many users have encountered! The issue happens because plotnine's default axis range calculation doesn't automatically add a small buffer around your data, so the extreme x/y values end up sitting right at the edges of the plot.
Here are a few simple fixes you can apply to your code:
Manually set axis limits with
xlim()/ylim()
You can explicitly define a slightly wider range than your data to create padding:p = ggplot(aes(x,y), gg) + geom_point() + xlim(-5.5, 5.5) + ylim(-1.5, 1.5)This pushes the edges out just enough so your data points aren't squashed against the plot borders.
Use
scale_x_continuous()/scale_y_continuous()withexpandparameter
This is a more flexible approach that adds a proportional buffer automatically, so you don't have to calculate exact limits:p = ggplot(aes(x,y), gg) + geom_point() + \ scale_x_continuous(expand=(0.05, 0)) + \ scale_y_continuous(expand=(0.05, 0))The
(0.05, 0)means adding a 5% buffer to both ends of the axis (no fixed constant padding).Add padding with
expand_limits()
This method lets you extend the axis range beyond your data's min/max values:p = ggplot(aes(x,y), gg) + geom_point() + \ expand_limits(x=c(-5.5, 5.5), y=c(-1.5, 1.5))
After applying any of these fixes, both the Jupyter Lab preview and your saved test3.png will have clean, properly spaced axes without values hugging the edges.
内容的提问来源于stack exchange,提问作者bill fite

