Python 如何更改刻度的数量?
因此,我已经在SE上搜索了一段时间,但没有找到任何有用的东西。。。。。我有一个多年来最低和最高温度的数据框架Python 如何更改刻度的数量?,python,matplotlib,plot,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)
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]
您可以通过获取每月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()
不错!谢谢:)你也会碰巧知道为什么我不能移动我的传奇我正在做:plt.legend(['1','2015年创纪录高点','2015年创纪录低点'])
plt.legend(loc=4,frameon=False,numpoints=1,title='legend')
显示标签,但位置仍然在右上角,有三个分散点,没有标题:(