在堆叠的 matplotlib 图中可视化标记列

Visualize marker column in a stacked matplotlib plot

我想创建一个带有附加线型图的堆叠图,如下所示:

df = pd.DataFrame(data)
df = df[['seconds', 'marker', 'data1', 'data2', 'data3']]
ax = df.set_index('seconds').plot(kind='bar', stacked=True, alpha=set_alpha)
ax.xaxis.set_major_locator(plt.MaxNLocator(5))
plt.plot(df.index, df['data1'], linestyle='solid', color='blue', alpha=0.4, label='data1')
plt.show()

示例数据:

seconds,marker,data1,data2,data3
00001,A,3,3,0,42,0
00002,B,3,3,0,34556,0
00003,C,3,3,0,42,0
00004,A,3,3,0,1833,0
00004,B,3,3,0,6569,0
00005,C,3,3,0,2454,0
00006,C,3,3,0,3256,0
00007,C,3,3,0,5423,0
00008,A,3,3,0,569,0

如何可视化第二列中的不同标记? 如果可能,可能在两个标记状态(B-A,B=开始,A=结束)之间建立视觉联系。

感谢@pasnik 我找到了一个解决方案:

dfmarkA=df.loc[df['marker']=='A']
dfmarkB=df.loc[df['marker']=='B']
dfmarkC=df.loc[df['marker']=='C']

dfmarkA['marker'] = dfmarkA['marker'].map({'A': scale * 1})
dfmarkB['marker'] = dfmarkB['marker'].map({'B': scale * 2})
dfmarkC['marker'] = dfmarkC['marker'].map({'C': scale * 3})


plt.plot(dfmarkA.index, dfmarkA['scale'], marker='*', linestyle='None', color='blue')
...

目前有警告,比例为固定值,还有改进空间。

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead