python-强制matplotlib在Y轴上仅显示从0开始的正数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-强制matplotlib在Y轴上仅显示从0开始的正数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2997字,纯文字阅读大概需要5分钟。
内容图文
![python-强制matplotlib在Y轴上仅显示从0开始的正数](/upload/InfoBanner/zyjiaocheng/680/2114554e82744575ae050e1ed1c31a95.jpg)
在一个循环中,我使用Matplotlib创建了一些子图.
我注意到在某些情况下,Y轴(尤其是在显示0时)在Y轴上显示了一些负数.
是否有一个参数可以强制Y轴显示正数(即从0开始向上显示).
还有一个参数,用于使Y轴的值等于或不等于小数位. (显示2而不是2.0或2.00(似乎会自动显示,具体取决于数据).
码:
for a in account_list:
f = plt.figure()
f.set_figheight(20)
f.set_figwidth(20)
f.sharex = True
f.sharey=True
left = 0.125 # the left side of the subplots of the figure
right = 0.9 # the right side of the subplots of the figure
bottom = 0.1 # the bottom of the subplots of the figure
top = 0.9 # the top of the subplots of the figure
wspace = 0.2 # the amount of width reserved for blank space between subplots
hspace = .8 # the amount of height reserved for white space between subplots
subplots_adjust(left=left, right=right, bottom=bottom, top=top, wspace=wspace, hspace=hspace)
count = 1
for h in headings:
sorted_data[sorted_data.account == a].ix[0:,['month_date',h]].plot(ax=f.add_subplot(7,3,count),legend=True,subplots=True,x='month_date',y=h)
import matplotlib.patches as mpatches
legend_name = mpatches.Patch(color='none', label=h)
plt.xlabel("")
ppl.legend(handles=[legend_name],bbox_to_anchor=(0.,1.2,1.0,.10), loc="center",ncol=2, mode="expand", borderaxespad=0.)
count = count + 1
编辑
当我使用以下方法设置Y轴的最小值和最大值时:我最终得到重复的数字.例如,过去在Y轴上具有的图:1、1.5、2、2.5,现在它只有1、1、2、2、3、3(我希望每个数字只出现一次.
可能与我的if语句不起作用有关.我希望所有最大Y轴值小于10的图都将其最大Y值设置为10.
#set bottom Y axis limit to 0 and change number format to 1 dec place.
axis_data = f.gca()
from matplotlib.ticker import FormatStrFormatter
axis_data.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
#set only integers on Y axis >>>
import matplotlib.ticker as ticker
axis_data.xaxis.set_major_locator(ticker.MaxNLocator(integer=True))
#if y max < 10 make it ten
ymin,ymax = axis_data.get_ylim()
print(type(ymax))
if ymax <10:
axis_data.set_ylim(bottom=0.,top=ymax)
else:
axis_data.set_ylim(bottom=0.)
解决方法:
ax.set_ylim是用于更改子图的y轴限制的方法.为了使用它,您必须拥有一个Axes实例(由plt.subplot返回的实例);那么您可以将下限设置为0.以下几行应该可以起作用:
ax = plt.subplot(1, 1, 1)
ax.plot(x, y)
ax.set_ylim(bottom=0.)
plt.show()
要更改刻度标签的格式,一种便捷的方法是使用FormatStrFormatter:
from matplotlib.ticker import FormatStrFormatter
ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
编辑:
我看过你的代码;您可以使用fig.gca()方法获取Axes实例,然后如上所述设置ylim和major_formatter.
编辑2:
可以通过指定ticks locator解决此问题,该注释可以解决.默认使用的定位器为AutoLocator,本质上是具有默认值的MaxNLocator. MaxNLocator使用的关键字之一是整数,它仅指定刻度的整数值,默认情况下为False.因此,要使刻度线仅采用整数值,您需要使用integer = True为y轴定义MaxNLocator.这应该工作:
from matplotlib.ticker import MaxNLocator
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
编辑3:
如果要将y上限设置为10,则应将其显式设置为10:
if ymax < 10:
axis_data.set_ylim(bottom=0., top=10.)
else:
axis_data.set_ylim(bottom=0.)
内容总结
以上是互联网集市为您收集整理的python-强制matplotlib在Y轴上仅显示从0开始的正数全部内容,希望文章能够帮你解决python-强制matplotlib在Y轴上仅显示从0开始的正数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。