如何在matplotlib中自动缩放y轴? [英] How to autoscale y axis in matplotlib?

查看:63
本文介绍了如何在matplotlib中自动缩放y轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建许多图,有些看起来很棒,而其他需要一些调整.从下面我怎样才能使难以看到的情节线更容易看到而不必手动绘制它们?我一次绘制了50-100张,然后将它们添加到pdf报告中.我想在线下添加空间,例如将 ylim min limit 设置为 -0.1,但会自动执行.

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:

这是很容易看到的情节线:

This one is easy to see plot line:

这是我的绘图代码:

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    

推荐答案

您要边距 doc

ax.margins(y=.1)

另见添加当图与图的边缘相对时的边距

这篇关于如何在matplotlib中自动缩放y轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆