Axis tick tags are truncated using pandas / matplotlib plot

I am using pandas (matplotlib in background) to generate some kind of bar graph.

I do it something like this:

output = StringIO.StringIO()
plot_canvas = matplotlib.pyplot.figure()
ax = plot_canvas.add_subplot(1, 1, 1)

series = df[key].value_counts()[:nums]
fig = series.plot(ax=ax, kind="bar", colormap="Blues",
                  legend=False,
                  stacked=False,
                  title="Numbers of %s" % key,
                  grid=True).get_figure()
fig.savefig(output, format="png")
o_data = output.getvalue()
output.close()
image_data = o_data.encode("base64")
image_uri = 'data:image/png;base64,%s' % image_data

      

My problem is that the X data is names (strings) which are quite long and which with these default settings are truncated into PNG. So I am wondering how I can move the graph in the graph to get more free space left without the graph.

enter image description here

You can see the lines on the X axis are being truncated.

+3


source to share


1 answer


There is a simple automatic solution :

plot_canvas.tight_layout()



You can also define an axis rectangle when creating a subheading by passing a keyword argument rect

to the add_subplot

format [left, bottom, width, height]

.

+1


source







All Articles