Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to autoscale y axis in matplotlib? [duplicate]

Tags:

matplotlib

I'm currently creating many plots and some look great while other need some adjustment. From below how can I make the hard to see plot line easier to see without having to manually plot them? I plot 50-100 of these at a time then add them to a pdf report. I'd like to add space under the line, for example have ylim min limit set to -0.1, but do it automatically.

This one is hard to see plot line: Hard to see plot line

This one is easy to see plot line: Easy to see plot line

Here is my code for plotting:

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    
like image 524
tlab Avatar asked Mar 13 '13 02:03

tlab


People also ask

How do I scale the y-axis in matplotlib?

Plot the x and y data points using numpy. Set the exponential scale for the Y-axis, using plt. yscale('symlog'). To display the figure, use show() method.

How do you autoscale a plot in Python?

Autoscale the axis view to the data (toggle). Convenience method for simple axis view autoscaling. It turns autoscaling on or off, and then, if autoscaling for either axis is on, it performs the autoscaling on the specified axis or axes.


1 Answers

You want margins doc

ex

ax.margins(y=.1)

Also see Add margin when plots run against the edge of the graph

like image 130
tacaswell Avatar answered Oct 23 '22 23:10

tacaswell