在 matplotlib 中更改 X(时间,而不是数字)频率上的滴答频率 [英] Change tick frequency on X (time, not number) frequency in matplotlib

查看:33
本文介绍了在 matplotlib 中更改 X(时间,而不是数字)频率上的滴答频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 python 绘图数据仅在 x 轴上显示 2 个点.

我想要更多,但不知道如何.

x = [ datetime.datetime(1900,1,1,0,1,2),datetime.datetime(1900,1,1,0,1,3),...] #(超过1000个元素)y = [ 34, 33, 23, ...... ]plt.plot( x, y )

X 轴仅显示 2 个间隔点.我尝试使用 .xticks 但不适用于 X 轴.它给出了以下错误:

TypeError: 'datetime.datetime' 类型的对象没有 len()

解决方案

无论是什么原因,默认情况下您只获得 2 个滴答声,您都可以通过使用日期定位器更改股票行情定位器来修复它(自定义).

>

将 matplotlib.pyplot 导入为 plt导入 matplotlib.dates 作为 mdatesx = [ 日期时间.日期时间(1900,1,1,0,1,2),datetime.datetime(1900,1,1,0,1,3),...] #(超过1000个元素)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)) #每15分钟获取一个tickax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) #可选格式

您在文档中阅读了多个定位器(例如: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆