yos*_*rry 6 python visualization
我可视化的数据只有在整数时才有意义.
即,就我正在分析的信息的背景而言,记录的0.2是没有意义的.
如何强制matplotlib仅使用Y轴上的整数.即1,100,5等?不是0.1,0.2等
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)
#set bottom Y axis limit to 0 and change number format to 1 dec place.
axis_data = f.gca()
axis_data.set_ylim(bottom=0.)
from matplotlib.ticker import FormatStrFormatter
axis_data.yaxis.set_major_formatter(FormatStrFormatter('%.0f'))
#This was meant to set Y axis to integer???
y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
axis_data.yaxis.set_major_formatter(y_formatter)
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
savefig(a + '.png', bbox_inches='tight')
Run Code Online (Sandbox Code Playgroud)
Joe*_*ton 12
最灵活的方法是指定integer=True默认的tick locator(MaxNLocator)做类似的事情:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
fig, ax = plt.subplots()
# Be sure to only pick integer tick locations.
for axis in [ax.xaxis, ax.yaxis]:
axis.set_major_locator(ticker.MaxNLocator(integer=True))
# Plot anything (note the non-integer min-max values)...
x = np.linspace(-0.1, np.pi, 100)
ax.plot(0.5 * x, 22.8 * np.cos(3 * x), color='black')
# Just for appearance's sake
ax.margins(0.05)
ax.axis('tight')
fig.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)

或者,您可以手动设置刻度位置/标签,如Marcin和Joel建议(或使用a MultipleLocator).这样做的缺点是你需要确定哪些刻度位置有意义,而不是让matplotlib根据轴限制选择合理的整数刻度间隔.
Ser*_*gey 12
强制整数刻度的另一种方法是使用pyplot.locator_params.
使用与接受的答案几乎相同的示例:
import numpy as np
import matplotlib.pyplot as plt
# Plot anything (note the non-integer min-max values)...
x = np.linspace(-0.1, np.pi, 100)
plt.plot(0.5 * x, 22.8 * np.cos(3 * x), color='black')
# use axis={'both', 'x', 'y'} to choose axis
plt.locator_params(axis="both", integer=True, tight=True)
# Just for appearance's sake
plt.margins(0.05)
plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)
如果它只是您想要更改的 yaxis,一个简单的方法是确定您想要的刻度:
tickpos = [0,1,4,6]
py.yticks(tickpos,tickpos)
Run Code Online (Sandbox Code Playgroud)
将在 0、1、4 和 6 处打勾。更一般地说
py.yticks([0,1,2,3], ['zero', 1, 'two', 3.0])
Run Code Online (Sandbox Code Playgroud)
会将第二个列表的标签放在第一个列表中的位置。如果标签将是 y 值,最好使用该py.yticks(tickpos,tickpos)版本以确保每当您更改刻度的位置时,标签都会得到相同的更改。
更一般地说,金顿的答案会让你告诉 pylab 只是 y 轴的整数,但让它选择刻度线的位置。
您可以按如下方式修改刻度标签/数字。这仅是示例,因为您尚未提供任何代码,因此不确定它是否适用于您。
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fig.canvas.draw()
# just the original labels/numbers and modify them, e.g. multiply by 100
# and define new format for them.
labels = ["{:0.0f}".format(float(item.get_text())*100)
for item in ax.get_xticklabels()]
ax.set_xticklabels(labels)
plt.show()
Run Code Online (Sandbox Code Playgroud)
不修改x轴:

修改后:
