optimus_prime
optimus_prime

Reputation: 827

X axes labels always get cut off for matplotlib subplots on a web page

I have plotted matplotlib subplots generated from a pandas dataframe on a web page using Flask. Here's the relevant code.

import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from io import BytesIO,StringIO
plt.style.use('ggplot')
plt.rcParams['xtick.color']='k'
plt.rcParams['xtick.labelsize']='x-large'
travel_df_new.unstack(level=0).plot(kind='bar', subplots=True, legend=False)
buff = BytesIO()
plt.savefig(buff, format='png', dpi=100)
buff.seek(0)

The x axes labels always tend to get cut off , even if I set plt.rcParams['xtick.labelsize']='x-small'.

Also, haven't found a way to rotate xtick labels inside rcParams.

My image is not covering the entire web page, so not sure if I need rotation.

enter image description here

Upvotes: 0

Views: 2727

Answers (1)

ml4294
ml4294

Reputation: 2629

You can try plt.tight_layout() to fix the cropped x labels. Normally, you can set the y limits of a plot with ylim(0, 400). If you have subplots instead of a plot, which I assume is the case here, maybe set_ylim(0,400). Maybe you will find some further help here: How to set axes limits in each subplot

Upvotes: 1

Related Questions