如何使用Matplotlib的可读图中的数千个依赖日期的数据点格式化?

发布于 2025-01-25 05:28:14 字数 753 浏览 2 评论 0原文

我有两个相应的列表,其中一个日期和另一个价格。列表长30,000个数据点。

x = [datetime.date(1997, 8, 8), datetime.date(2021, 8, 17), datetime.date(2019, 8, 7), ... ]

y = [0.49, 1.99, 0.0, ...]

我正在使用此代码生成以下图表:

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y/%m/%d'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=5))
plt.scatter(x,y)
plt.gcf().autofmt_xdate()
plt.show()

​我也不确定是否有可能使图形更可读性,或者这是具有如此多数据点的散点的本质(不确定还有什么使用)。

日期从1997年到2022年不等,价格从0.0(免费)到300。

此外,我遇到的错误即使在图表显示的时候也会不断弹出,我不知道它在做什么:

试图生成1995 tick的定位器([9591.0,...,19561.0]),它超过了定位器。

I have two corresponding lists, one of the dates and another of its respective price. The lists are 30,000 data points long.

x = [datetime.date(1997, 8, 8), datetime.date(2021, 8, 17), datetime.date(2019, 8, 7), ... ]

y = [0.49, 1.99, 0.0, ...]

I'm using this code to generate the graph below:

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y/%m/%d'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=5))
plt.scatter(x,y)
plt.gcf().autofmt_xdate()
plt.show()

enter image description here

I'm supposed to have the dates separated in a readable format using the code above but it doesn't seem to work. I'm also not sure if it's possible to make the graph more readable or if that's the nature of a scatter with so many data points (not sure what else to use).

The dates range from 1997 to 2022 and the prices from 0.0 (free) to 300.

Also, I'm getting this error that constantly pops up even while the graph is showing and I have no idea what it's doing:

Locator attempting to generate 1995 ticks ([9591.0, ..., 19561.0]), which exceeds Locator.MAXTICKS (1000).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

静待花开 2025-02-01 05:28:15

如果您希望图形更可读取,则需要将日常对象的间隔增加到至少6000。代码> scation函数的参数:

import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime
import matplotlib.dates as mdates

x = pd.date_range(datetime.today(), periods=30000).tolist()
y = list(range(30000))

plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=6000))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
plt.gcf().autofmt_xdate()
plt.scatter(x, y, s=0.1)

输出:

”在此处输入图像描述”

If you want your graph to be more readable, you need to increase the interval of DayLocator object to at least 6000. Also, you can change the size of the scatter points with the s parameter at the scatter function:

import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime
import matplotlib.dates as mdates

x = pd.date_range(datetime.today(), periods=30000).tolist()
y = list(range(30000))

plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=6000))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
plt.gcf().autofmt_xdate()
plt.scatter(x, y, s=0.1)

Output:

enter image description here

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