我有一个数据框,索引作为日期。列是不同的时间序列,我添加了另一列来标记特定观察属于哪个工作日。像这样:
我想做的是:
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')
但是,我无法在第一个数字中添加图例。对于第二个图,我无法添加不同颜色的标记或图例。 有人可以帮忙。
b)如果我能达到a),那么我想在同一个数字上绘制所有三个系列。
谢谢!
答案 0 :(得分:1)
是的,这一切都是可能的!所以首先让我们在同一个情节中获得所有三个时间序列
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()
如果您想确保标记位于线条的前面以获得更清晰的图形,我们可以使用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()
您可以使用颜色栏显示天数差异
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()
如果您希望图例包含与不同日期相关联的颜色,则可以制作自定义色彩映射,然后将自定义图例设为
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()