显示倒置的垂直日期轴
我试图制作的图表是一个二维数组,以日期作为其垂直维度。按照惯例,日期应该从上到下递增。使用此代码以其他方式显示日期效果很好:
import numpy as np
import matplotlib as mpl
import matplotlib.colorbar as cb
import matplotlib.pyplot as plt
from datetime import datetime
y = np.reshape(np.random.rand(100), (10, 10))
f1 = plt.figure()
ax1 = f1.add_axes([0.15, 0.15, 0.76, 0.80])
mindate = mpl.dates.date2num(datetime(2010, 1, 10))
maxdate = mpl.dates.date2num(datetime(2010, 1, 20))
im1 = ax1.imshow(y, cmap='binary', aspect='auto', origin='upper',
interpolation='nearest', extent=(0, 1, mindate, maxdate))
ax1.yaxis_date()
plt.show()
对于其他数据类型,通过切换间隔边界来反转轴的方向非常简单,但对于日期,这会导致空 Y 轴。有没有一种方法可以在不编写自定义刻度定位器的情况下使其正常工作?
The chart I'm trying to make is a 2D-array with date as its vertical dimension. By convention, the dates should increase from the top down. Displaying the date the other way around works fine with this code:
import numpy as np
import matplotlib as mpl
import matplotlib.colorbar as cb
import matplotlib.pyplot as plt
from datetime import datetime
y = np.reshape(np.random.rand(100), (10, 10))
f1 = plt.figure()
ax1 = f1.add_axes([0.15, 0.15, 0.76, 0.80])
mindate = mpl.dates.date2num(datetime(2010, 1, 10))
maxdate = mpl.dates.date2num(datetime(2010, 1, 20))
im1 = ax1.imshow(y, cmap='binary', aspect='auto', origin='upper',
interpolation='nearest', extent=(0, 1, mindate, maxdate))
ax1.yaxis_date()
plt.show()
Reversing the direction of an axis is quite simple with other data types by switching interval boundaries, but with date this results in an empty Y axis. Is there a way to get this working without writing a custom tick locator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,反转日期时间轴会消除刻度定位器和格式设置。我能想到的最简单的事情就是手动重新设置它们。以下是格式表的链接。这是 DateFormatter 的链接。您也可以使用 AutoDateFormatter。
这是代码:
它产生以下内容:
It's too bad that inverting a datetime axis obliterates the ticklocator and formatting settings. The easiest thing I can figure is to manually re-set them. Here's a link to the format table. Here's a link to DateFormatter. You could also probably use
AutoDateFormatter
.Here's the code:
It produces this: