我想这是一个简单的问题,但我已经尝试了几个小时,但没有结果。我想在matplotlib的图表中看到一条数据
,
。
_mat_graph =
{'AFG': [(datetime.datetime(2021, 2, 22, 0, 0), 0.0),
(datetime.datetime(2021, 2, 28, 0, 0), 8200.0),
(datetime.datetime(2021, 3, 16, 0, 0), 54000.0),
(datetime.datetime(2021, 4, 7, 0, 0), 120000.0),
(datetime.datetime(2021, 4, 22, 0, 0), 240000.0),
(datetime.datetime(2021, 5, 11, 0, 0), 448878.0)]
'ALB': [(datetime.datetime(2021, 1, 10, 0, 0), 0.0),
(datetime.datetime(2021, 1, 12, 0, 0), 128.0),
(datetime.datetime(2021, 1, 13, 0, 0), 188.0),
(datetime.datetime(2021, 1, 14, 0, 0), 266.0),
(datetime.datetime(2021, 1, 15, 0, 0), 308.0),
(datetime.datetime(2021, 1, 16, 0, 0), 369.0),
(datetime.datetime(2021, 1, 17, 0, 0), 405.0),
(datetime.datetime(2021, 1, 18, 0, 0), 447.0),
(datetime.datetime(2021, 1, 19, 0, 0), 483.0),
(datetime.datetime(2021, 1, 20, 0, 0), 519.0),
(datetime.datetime(2021, 1, 21, 0, 0), 549.0),
(datetime.datetime(2021, 2, 2, 0, 0), 549.0),
(datetime.datetime(2021, 2, 9, 0, 0), 689.0),
(datetime.datetime(2021, 2, 17, 0, 0), 1090.0),
(datetime.datetime(2021, 2, 18, 0, 0), 2438.0),
(datetime.datetime(2021, 2, 22, 0, 0), 6073.0),
(datetime.datetime(2021, 5, 11, 0, 0), 440921.0),
(datetime.datetime(2021, 5, 12, 0, 0), 444755.0),
(datetime.datetime(2021, 5, 13, 0, 0), 445402.0),
(datetime.datetime(2021, 5, 14, 0, 0), 448571.0)]
'AND': [(datetime.datetime(2021, 1, 25, 0, 0), 576.0),
(datetime.datetime(2021, 2, 1, 0, 0), 1036.0),
(datetime.datetime(2021, 2, 10, 0, 0), 1291.0),
(datetime.datetime(2021, 2, 12, 0, 0), 1622.0),
(datetime.datetime(2021, 2, 19, 0, 0), 2141.0),
(datetime.datetime(2021, 2, 24, 0, 0), 2390.0),
(datetime.datetime(2021, 3, 8, 0, 0), 2439.0),
(datetime.datetime(2021, 3, 10, 0, 0), 3650.0),
(datetime.datetime(2021, 3, 15, 0, 0), 7098.0),
(datetime.datetime(2021, 4, 5, 0, 0), 9781.0),
(datetime.datetime(2021, 4, 19, 0, 0), 21733.0),
(datetime.datetime(2021, 4, 26, 0, 0), 23822.0),
(datetime.datetime(2021, 5, 3, 0, 0), 24182.0),
(datetime.datetime(2021, 5, 10, 0, 0), 26931.0)]}现在,这是(不起作用的)我正在使用的代码(SAFE_COLORS是以十六进制表示的rgb颜色列表的一个片段):
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
from matplotlib.dates import drange
import numpy as np
cont = 0
for _iso, _entries in _mat_graph.items():
graph_dates = drange(_entries[0][0], _entries[-1][0], timedelta(days=1))
cantidades = np.array([x[1] for x in _entries])
plt.plot_date(
graph_dates,
cantidades,
color=SAFE_COLORS[12][cont],
marker='.'
)
cont += 1
plt.show()这就是我遇到的错误:
path\to\pyfiles\main.py:1055: UserWarning: marker is redundantly defined by the 'marker' keyword argument and the fmt string "o" (-> marker='o'). The keyword argument will take precedence.
plt.plot_date(
Traceback (most recent call last):
File "path\to\pyfiles\main.py", line 1055, in <module> plt.plot_date(
File "path\to\pyfiles\.venv\lib\site-packages\matplotlib\pyplot.py", line 3029, in plot_date
return gca().plot_date(
File "path\to\pyfiles\.venv\lib\site-packages\matplotlib\__init__.py", line 1361, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "path\to\pyfiles\.venv\lib\site-packages\matplotlib\axes\_axes.py", line 1676, in plot_date
return self.plot(x, y, fmt, **kwargs)
File "path\to\pyfiles\.venv\lib\site-packages\matplotlib\axes\_axes.py", line 1605, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "path\to\pyfiles\.venv\lib\site-packages\matplotlib\axes\_base.py", line 315, in __call__
yield from self._plot_args(this, kwargs)
File "path\to\pyfiles\.venv\lib\site-packages\matplotlib\axes\_base.py", line 501, in _plot_args
raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (78,) and (6,)如有任何帮助,我们将不胜感激,
发布于 2021-05-19 18:41:17
我不太清楚你想用上面的graph_dates做什么,但我猜这是你的问题。最简单的是:
fig, ax = plt.subplots(constrained_layout=True)
plt.rcParams['date.converter'] = 'concise'
for key, data in _mat_graph.items():
date = [x[0] for x in data]
data = [x[1] for x in data]
ax.plot(date, data, '.', ms=10, label=key)
ax.legend()
plt.show()

https://stackoverflow.com/questions/67608723
复制相似问题