Python 强制matplotlib在Y轴上仅显示从0开始的正数

Python 强制matplotlib在Y轴上仅显示从0开始的正数,python,matplotlib,visualization,Python,Matplotlib,Visualization,在一个循环中,我使用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

在一个循环中,我使用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.)
#将底部Y轴限制设置为0,并将数字格式更改为1 dec place。
axis_数据=f.gca()
从matplotlib.ticker导入FormatStrFormatter
axis_data.yaxis.set_major_格式化程序(FormatStrFormatter('%.0f'))
#仅设置Y轴上的整数>>>
将matplotlib.ticker导入为ticker
axis_data.xaxis.set_major_定位器(ticker.MaxNLocator(integer=True))
#如果y max<10,则为10
ymin,ymax=axis_data.get_ylim()
打印(类型(ymax))

如果ymax轴范围可以通过
xlim()
ylim()
限制在特定范围内,例如

ylim([0,2])
可以使用
xticks()
yticks()
将轴刻度手动设置为任何字符串,例如

yticks([0,1,2],["0","1","2"])
是更改子批次y轴限制的方法。要使用此方法,您必须拥有一个
Axes
实例(由
plt.subplot
返回的实例);然后您可以将下限设置为0。沿着这些线应该可以工作:

ax = plt.subplot(1, 1, 1)
ax.plot(x, y)
ax.set_ylim(bottom=0.)
plt.show()
from matplotlib.ticker import MaxNLocator
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
要更改记号标签格式,一种方便的方法是使用:

编辑: 我已经看过了您的代码;您可以使用方法获得一个
Axes
实例,然后如上所述设置ylim和major\u格式化程序

EDIT2: 这个问题解决了。注释可以通过指定来解决。默认情况下使用的定位器是,它本质上是带有默认值的
MaxNLocator
。所采用的关键字之一是integer,它只指定记号的整数值,默认情况下是
False
。因此,要使记号只采用整数值,请您需要使用
integer=True
定义y轴
MaxNLocator
。这应该可以:

ax = plt.subplot(1, 1, 1)
ax.plot(x, y)
ax.set_ylim(bottom=0.)
plt.show()
from matplotlib.ticker import MaxNLocator
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
EDIT3

如果要将y上限设置为10,则应明确将其设置为10:

if ymax < 10:
    axis_data.set_ylim(bottom=0., top=10.)
else:
    axis_data.set_ylim(bottom=0.)
如果ymax<10:
轴数据集ylim(底部=0,顶部=10。)
其他:
axis_data.set_ylim(底部=0。)

不幸的是,有很多方法可以调整刻度。因此,有时会变得一团糟

尝试:

如果在两个参数中都不使用tickpos,并且使用两个不同的数组,则第一个数组表示记号的位置。第二个数组表示记号标签的位置。如果您希望标签是字符串(假设它是条形图,并且希望标签是给定条形图的类别),这一点非常有用。如果您将它们作为数字,这是非常危险的,因为您可能会发现您移动了勾号位置,但没有移动标签。因此,您将位置y=4标记为,例如,6。以下情况将是不好的

py.yticks([1,2,3], [1, 4, -1])  #DO NOT DO THIS
您还可以设置 py轴(ymin=0,ymax=4,xmin=3)
或者其他变量,具体取决于您希望py自由设置的限制。请注意,有时我会设置ymax=4.000001,以确保y=4处的刻度出现。

一些代码示例您所拥有的将非常有用。感谢Marcin-添加了一些代码和示例输出。附带一点警告:设置类似于
tval的内容通常是一个好主意s=[0,1,2]
以便命令可以是
yticks(tvals,tvals)
。如果你发现自己编辑了勾号位置而忽略了更改勾号标签,事情会变得非常糟糕。保持列表不变可以防止出现这种错误。感谢@Andrey Sobolev,指定小数位数非常有效。有没有办法设置Y轴上的增量。我喜欢matplotlib计算跳转的方式在增量中,即100-200或1、5、10、15。但我想强制它使用整数(即0.5、1.5等)?是否可以强制Y轴上的整数?