如何更改刻度数?

时间:2017-04-08 01:30:13

标签: python matplotlib plot

所以我现在已经在SE上搜索了一段时间,但我找不到任何工作......我有多年来最低和最高温度的数据框架。

plt.figure()
plt.gca().fill_between(range(len(Tmin)), Tmin, Tmax, facecolor='blue', alpha=0.25)
plt.plot(range(365), Tmin, 'b', range(365), Tmax, 'r')
plt.locator_params(axis = 'x', nbins = 12)

x = plt.gca().xaxis
m = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
ax = plt.gca()
ax.locator_params(axis = 'x', nbins = 12)
ax.set_xticklabels(m)
ax.set_ylabel('T(C)')
ax.set_title('Temperature')
plt.xlim([0,365])
# ax.set_xticks(np.linspace(0,1,12))
plt.show()

它仍然输出与原始图中相同的刻度数,其中x轴为[0,50,100,...,350]

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以通过获取每月1月1日以来的日期来手动设置滴答。

import datetime as dt
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(365)
y = 100 + 250*np.sin(9*(x-50)/720) + 80*np.random.rand(365)

m = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
# get the start day for each month as an index
ticks = [(dt.date(2016,m,1)-d0).days for m in range(1,13)]

fig,ax = plt.subplots(1,1)
ax.plot(x,y)
ax.set_xticks(ticks)
ax.set_xticklabels(m)
ax.set_xlim(-3,365)
plt.show()

enter image description here