I’m fed up with manually creating graphs in excel and consequently, I’m trying to automate the process using Python to massage the .csv data into a workable form and matplotlib to plot the result.
Using matplotlib and generating them is no problem but I can’t work out is how to set the aspect ration / resolution of the output.
Specifically, I’m trying to generate scatter plots and stacked area graphs. Everything I’ve tried seems to result in one or more of the following:
- Cramped graph areas (small plot area covered with the legend, axes etc.).
- The wrong aspect ratio.
- Large spaces on the sides of the chart area (I want a very wide / not very tall image).
If anyone has some working examples showing how to achieve this result I’d be very grateful!
You can control the aspect ratio with
ax.set_aspect:Another way to set the aspect ratio (already mentioned by jterrace) is to use the
figsizeparameter inplt.figure: e.g.fig = plt.figure(figsize=(3,1)). The two methods are slightly different however: A figure can contain many axes.figsizecontrols the aspect ratio for the entire figure, whileset_aspectcontrols the aspect ratio for a particular axis.Next, you can trim unused space from around the plot with
bbox_inches='tight':If you want to get rid of the excess empty space due to matplotlib’s automated choice of x-range and y-range, you can manually set them with
ax.set_xlimandax.set_ylim: