在matplotlib中的X(时间,不是数字)频率上更改刻度频率 [英] Change tick frequency on X (time, not number) frequency in matplotlib
问题描述
我的python
图数据仅在x轴上显示2个点.
我想拥有更多,但不知道如何.
x = [ datetime.datetime(1900,1,1,0,1,2),
datetime.datetime(1900,1,1,0,1,3),
...
] # ( more than 1000 elements )
y = [ 34, 33, 23, ............ ]
plt.plot( x, y )
X轴仅显示2个间隔点.我尝试使用.xticks
,但不适用于X轴.
它给出了以下错误:
TypeError: object of type 'datetime.datetime' has no len()
无论是什么原因,默认情况下您只会获得2个滴答声,您都可以使用日期定位器来更改滴答声定位器,以进行修正(自定义).
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
x = [ datetime.datetime(1900,1,1,0,1,2),
datetime.datetime(1900,1,1,0,1,3),
...
] # ( more than 1000 elements )
y = [ 34, 33, 23, ............ ]
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
plt.plot( x, y )
ax.xaxis.set_major_locator(mdates.MinuteLocator(interval=15)) #to get a tick every 15 minutes
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) #optional formatting
您有几个定位器(例如:DayLocator,WeekdayLocator,MonthLocator等)在文档中对其进行了阅读:
http://matplotlib.org/api/dates_api.html
但是也许这个例子会有所帮助:
http://matplotlib.org/examples/api/date_demo.html
My python
plot data only show 2 points on x axis.
I would like to have more, but don't know how.
x = [ datetime.datetime(1900,1,1,0,1,2),
datetime.datetime(1900,1,1,0,1,3),
...
] # ( more than 1000 elements )
y = [ 34, 33, 23, ............ ]
plt.plot( x, y )
The X axis only shows 2 points of interval. I tried to use .xticks
but didn't work for X axis.
It gave the below error:
TypeError: object of type 'datetime.datetime' has no len()
Whatever reason it is you are getting 2 ticks only by default, you can fix it (customise it) by changing the ticker locator using a date locator.
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
x = [ datetime.datetime(1900,1,1,0,1,2),
datetime.datetime(1900,1,1,0,1,3),
...
] # ( more than 1000 elements )
y = [ 34, 33, 23, ............ ]
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
plt.plot( x, y )
ax.xaxis.set_major_locator(mdates.MinuteLocator(interval=15)) #to get a tick every 15 minutes
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) #optional formatting
You have several locators (for example: DayLocator, WeekdayLocator, MonthLocator, etc.) read about it in the documentation:
http://matplotlib.org/api/dates_api.html
But maybe this example will help more:
http://matplotlib.org/examples/api/date_demo.html
这篇关于在matplotlib中的X(时间,不是数字)频率上更改刻度频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!