简体   繁体   English

如何使用 Matplotlib 在可读图形中格式化数千个日期相关数据点?

[英]How can I format multiple thousands of date-dependant data points in a readable graph with Matplotlib?

I have two corresponding lists, one of the dates and another of its respective price.我有两个相应的列表,一个是日期,另一个是相应的价格。 The lists are 30,000 data points long.这些列表有 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, ...]

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

在此处输入图像描述

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.日期范围从 1997 年到 2022 年,价格从 0.0(免费)到 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).定位器试图生成 1995 个刻度 ([9591.0, ..., 19561.0]),这超过了 Locator.MAXTICKS (1000)。

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:如果你想让你的图表更具可读性,你需要将DayLocator object 的间隔增加到至少 6000。另外,你可以在scatter点 function 处用s参数改变散点的大小:

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: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM