使用panda和matplotlib设置xtick最大值

时间:2014-05-08 00:43:37

标签: python matplotlib pandas

最后一个xtick怎么可能是数据的最后一个值?

import pandas as pd
import matplotlib.pyplot as plt
a = [583, 1379, 1404, 5442, 5512, 5976, 5992, 6111, 6239, 6375, 6793, 6994, 7109, 7149, 7210, 7225, 7459, 8574, 9154, 9417, 9894, 10119, 10355, 10527, 11933, 12170, 12172, 12273, 12870, 13215, 13317, 13618, 13632, 13790, 14260, 14282, 14726, 15156, 19262, 21501, 21544, 21547, 21818, 21939, 22386, 22622, 22830, 23898, 25796, 26126, 26340, 28585, 28645, 28797, 28808, 28878, 29167, 29168, 31161, 31225, 32284, 32332, 32641, 33227, 34175, 34349, 34675, 34772, 34935, 35086]

d = pd.DataFrame(a)
d.hist(bins=50)
f = plt.figure()
plt.xlabel("xTEST")
plt.ylabel("yTEST")
plt.subplots_adjust(bottom=.25, left=.25)

plt.ticklabel_format(style = 'plain')

plt.title('Title here!', color='black')
plt.xlim(xmin=1, xmax=35086)
plt.show()

2 个答案:

答案 0 :(得分:1)

不确定您需要什么,但您需要删除该行

f = plt.figure()

如果您希望直方图d和标题/标签/ xlim等位于同一图表中。

答案 1 :(得分:1)

如果您想使用由pandas创建的相同图形,则无法创建新图形(请参见下文)。您可以在plt.xticks()

之后手动添加plt.xlim()来设置滴答位置
plt.xticks(seq)

例如,序列seq可以是pd.np.linspace(1, 35086, 5),或者手动给出,如:

seq = [1, 10000, 20000, 35086]

最终的代码是:

import pandas as pd
import matplotlib.pyplot as plt
a = [583, 1379, 1404, 5442, 5512, 5976, 5992, 6111, 6239, 6375, 6793, 6994, 7109, 7149, 7210, 7225, 7459, 8574, 9154, 9417, 9894, 10119, 10355, 10527, 11933, 12170, 12172, 12273, 12870, 13215, 13317, 13618, 13632, 13790, 14260, 14282, 14726, 15156, 19262, 21501, 21544, 21547, 21818, 21939, 22386, 22622, 22830, 23898, 25796, 26126, 26340, 28585, 28645, 28797, 28808, 28878, 29167, 29168, 31161, 31225, 32284, 32332, 32641, 33227, 34175, 34349, 34675, 34772, 34935, 35086]

d = pd.DataFrame(a)
d.hist(bins=50)
ax = plt.gca()
ax.set_xlabel("xTEST")
ax.set_ylabel("yTEST")
plt.subplots_adjust(bottom=.25, left=.25)
plt.ticklabel_format(style = 'plain')

ax.set_title('Title here!', color='black')
ax.set_xlim(xmin=1, xmax=35086)
ax.set_xticks(pd.np.linspace(1, 35086, 5))
plt.show()

,并提供:

enter image description here