显示倒置的垂直日期轴

发布于 2024-11-03 18:42:11 字数 702 浏览 3 评论 0原文

我试图制作的图表是一个二维数组,以日期作为其垂直维度。按照惯例,日期应该从上到下递增。使用此代码以其他方式显示日期效果很好:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤者何惧 2024-11-10 18:42:11

不幸的是,反转日期时间轴会消除刻度定位器和格式设置。我能想到的最简单的事情就是手动重新设置它们。以下是格式表的链接。这是 DateFormatter 的链接。您也可以使用 AutoDateFormatter。

这是代码:

import numpy as np
import matplotlib as mpl
import matplotlib.colorbar as cb
import matplotlib.pyplot as plt
from datetime import datetime
from matplotlib import dates

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()

ax1.invert_yaxis()
hfmt = dates.DateFormatter('%b %d %Y')
ax1.yaxis.set_major_locator(dates.DayLocator(interval=1))
ax1.yaxis.set_major_formatter(hfmt)

plt.show()

它产生以下内容:在此处输入图像描述

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:

import numpy as np
import matplotlib as mpl
import matplotlib.colorbar as cb
import matplotlib.pyplot as plt
from datetime import datetime
from matplotlib import dates

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()

ax1.invert_yaxis()
hfmt = dates.DateFormatter('%b %d %Y')
ax1.yaxis.set_major_locator(dates.DayLocator(interval=1))
ax1.yaxis.set_major_formatter(hfmt)

plt.show()

It produces this:enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文