gpt4 book ai didi

python - 使用按工作日和图例着色的标记绘制时间序列

转载 作者:行者123 更新时间:2023-12-01 09:34:14 26 4
gpt4 key购买 nike

我有一个以索引作为日期的数据框。这些列是不同的时间序列,我添加了另一列来标记特定观察属于哪个工作日。像这样的事情:

enter image description here

我想做的是:

a) 绘制时间序列(例如系列 1),向图中添加标记并按工作日着色。我使用 plt.scatter 和 plt.plot 得到了 2 个图:

plt.scatter(x = df.index, y = df['Series1'], c = df['day'])
plt.plot(df.index, df['Series1'], marker = 'o')

enter image description here enter image description here

但是,我无法为第一个图形添加图例。对于第二个图,我无法添加不同颜色的标记或图例。有人可以帮忙吗?

b) 如果我可以实现 a),那么我想将所有三个系列绘制在同一个图上。

谢谢!

最佳答案

是的,这一切皆有可能!因此,首先让我们将所有三个时间序列放在同一个图上

import matplotlib.pyplot as plt

plt.figure()
plt.subplot(1,1,1)
line1, = plt.plot(df.index, df['Series1'])
line2, = plt.plot(df.index, df['Series2'])
line3, = plt.plot(df.index, df['Series3'])

plt.scatter(df.index, df['Series1'], c = df['day'], marker = 'o')
plt.scatter(df.index, df['Series2'], c = df['day'], marker = 'v')
plt.scatter(df.index, df['Series3'], c = df['day'], marker = 'x')

plt.legend(handles=[line1, line2, line3])
plt.show()

enter image description here

<小时/>

如果您想确保标记位于线条前面以获得更清晰的绘图,我们可以使用 zorder 属性。

import matplotlib.pyplot as plt

plt.figure()
plt.subplot(1,1,1)
line1, = plt.plot(df.index, df['Series1'], zorder=1)
line2, = plt.plot(df.index, df['Series2'], zorder=1)
line3, = plt.plot(df.index, df['Series3'], zorder=1)

plt.scatter(df.index, df['Series1'], c = df['day'], marker = 'o', s = 100, zorder=2)
plt.scatter(df.index, df['Series2'], c = df['day'], marker = 'v', s = 100, zorder=2)
plt.scatter(df.index, df['Series3'], c = df['day'], marker = 'x', s = 100, zorder=2)

plt.legend(handles=[line1, line2, line3])
plt.show()

enter image description here

<小时/>

您可以使用颜色条显示天数差异

import matplotlib.pyplot as plt

plt.figure()
plt.subplot(1,1,1)
line1, = plt.plot(df.index, df['Series1'], zorder=1)
line2, = plt.plot(df.index, df['Series2'], zorder=1)
line3, = plt.plot(df.index, df['Series3'], zorder=1)

plt.scatter(df.index, df['Series1'], c = df['day'], marker = 'o', s = 100, zorder=2)
plt.scatter(df.index, df['Series2'], c = df['day'], marker = 'v', s = 100, zorder=2)
plt.scatter(df.index, df['Series3'], c = df['day'], marker = 'x', s = 100, zorder=2)

plt.legend(handles=[line1, line2, line3])
plt.colorbar()
plt.show()

enter image description here

<小时/>

如果您希望图例包含与不同日期相关的颜色,那么您可以制作自定义颜色图,然后将自定义图例设置为

from matplotlib.patches import Patch
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt

# define the colormap
cmap = plt.cm.jet
cmaplist = [cmap(i) for i in range(1,cmap.N,cmap.N//max(df['day']))]

plt.figure()
plt.subplot(1,1,1)
line1, = plt.plot(df.index, df['Series1'], zorder=1)
line2, = plt.plot(df.index, df['Series2'], zorder=1)
line3, = plt.plot(df.index, df['Series3'], zorder=1)

plt.scatter(df.index, df['Series1'], c = df['day'], cmap='jet', marker = 'o', s = 100, zorder=2)
plt.scatter(df.index, df['Series2'], c = df['day'], cmap='jet', marker = 'v', s = 100, zorder=2)
plt.scatter(df.index, df['Series3'], c = df['day'], cmap='jet', marker = 'x', s = 100, zorder=2)

legend_elements = [Line2D([0], [0], color='b', lw=4, label='Line'),
Line2D([0], [0], marker='o', color='w', label='Scatter',
markerfacecolor='g', markersize=15),
Patch(facecolor='orange', edgecolor='r',
label='Color Patch')]
legend_elements = []
for ix, i in enumerate(df['day']):
temp = Line2D([0], [0], color = cmaplist[i-1][0:3], lw=4, label=str(i))
legend_elements.append(temp)


plt.legend(handles=legend_elements)
plt.show()

enter image description here

关于python - 使用按工作日和图例着色的标记绘制时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49675785/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com