[英]How can I add data points to my line plot in same colour (matplotlib)?
I have a line plot to which I would like to add original datapoints in the same colour as the lines (which are fine in default). 我有一个线条图,我想向其添加与线条相同颜色的原始数据点(默认情况下很好)。 Problem: When I do it for a lot of IDs or datapoints (also missings) I can no longer distinguish to whom that data belong.
问题:当我为许多ID或数据点(也有缺失)执行此操作时,我无法再区分该数据属于谁。
import pandas as pd
df = pd.DataFrame({'year': ['1988', '1989', '1990', '1988', '1989', '1990', '1988', '1989', '1990'],
'id': ['1', '1', '1', '2', '2', '2', '3', '3', '3'],
'money': ['5', '7', '8', '8', '3', '3', '7', '8', '10']}).astype(int)
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
for i in df.id.unique():
df[df['id']==i].plot.line(x='year', y='money', ax=ax, label='id = %s'%i)
df[df['id']==i].plot.scatter(x='year', y='money', ax=ax)
plt.xticks(np.unique(df.year))
Question: How can I access the default color list of my plot.line
to use it in plot.scatter
? 问题:如何访问
plot.line
的默认颜色列表以在plot.scatter
使用它? Or is there another, easier way to highlight the data which constitutes the lines? 还是有另一种更容易的方式来突出显示构成线条的数据?
IIUC, you can try, although @QuangHoang is the better solution: IIUC,您可以尝试,尽管@QuangHoang是更好的解决方案:
import pandas as pd
df = pd.DataFrame({'year': ['1988', '1989', '1990', '1988', '1989', '1990', '1988', '1989', '1990'],
'id': ['1', '1', '1', '2', '2', '2', '3', '3', '3'],
'money': ['5', '7', '8', '8', '3', '3', '7', '8', '10']}).astype(int)
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
color = plt.rcParams['axes.prop_cycle'].by_key()['color']
for i in df.id.unique():
df[df['id']==i].plot.line(x='year', y='money', ax=ax, label='id = %s'%i)
df[df['id']==i].plot.scatter(x='year', y='money', ax=ax, color=color[i-1])
plt.xticks(np.unique(df.year))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.