How to autoscale y axis in matplotlib?

I am currently creating a lot of stories, and some look great, while others need some adjustment. Below, how can I make it difficult to see the storyline easier to see without forcing them to be drawn manually? I compose 50-100 of them at a time, and then add them to the pdf report. I would like to add space under the line, for example, the ylim min limit is set to -0.1, but do it automatically.

It's hard to see the storyline: Hard to see plot line

It is easy to see the storyline: Easy to see plot line

Here is my build code:

def plot(chan_data): '''Uses matplotlib to plot a channel ''' f, ax = plt.subplots(1, figsize=(8, 2.5)) x = dffinal['time'].keys() ax.plot(x, dffinal[chan_data].values, linewidth=0.4, color='blue') ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y - %H:%M')) ax.xaxis.set_major_locator(mdates.AutoDateLocator(interval_multiples=True)) lgd1 = ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) f.autofmt_xdate() ax.set_ylabel(dffinal[chan_data].name) ax.grid('on') #I've tried these with no luck #ax.autoscale(enable=True, axis='y', tight=False) #ax.set_ymargin(0.5) #ax.set_autoscaley_on(True) fname = ".\\plots\\" + chan_data + ".png" print "Creating: " + fname plt.savefig(fname, dpi=100, bbox_extra_artist=(lgd1,), bbox_inches='tight') plt.close() return fname 
+4
source share
1 answer

Do you want margins doc

ex

 ax.margins(y=.1) 

Also see Add Margin when charts run against the edge of the chart.

+12
source

All Articles