不同图之间的线是否保持相同的颜色顺序?我删除了一行,颜色现在重新排列 [英] Keep the same color order for lines between different plots? I dropped one line, colors are now rearranged

查看:25
本文介绍了不同图之间的线是否保持相同的颜色顺序?我删除了一行,颜色现在重新排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pandas数据框,想比较两条线图或意大利面条"图.第二个图从数据框中删除了一列.

然而,当颜色在图之间重新排列时会有点混乱.

例如,八家商店的每周销售额:

(df.plot(figsize=(20,10),lw=2.5,linestyle='--').legend(loc=2, prop={'size': 13}))

我可以看到 shop1(蓝色)淹没了其他商店.所以我决定删除shop1,然后重新绘制:

# drop shop 1df.drop('shop1',axis=1,inplace=True)#再次绘图(df.plot(figsize =(20,10),lw = 2.5,linestyle ='-').legend(loc = 2,prop = {'size':13}))

现在颜色已经重新排列了.Shop2是橙色,但现在是绿色.所有颜色均已转移​​.

是否有一种简单的方法来保存图表之间每个商店的颜色?

我已经测试了一个小时的不同方法来解决这个问题.使用 pop 摆脱线对象,试图侵入matplotlib color_cycle ,我什至试图通过将值设置为零(白色)来隐藏线.

示例数据帧,用于pd.DataFrame.from_dict():

  df.to_dict(){'shop1':{'2020-01-06':9778.763579802846,'2020-01-13':10294.040674742606,'2020-01-20':10748.72889467783,'2020-01-27':9995.956972783448,'2020-02-03':11013.304192764444,'2020-02-10':13165.999999999907,'2020-02-17':11180.50000000096,'2020-02-24':9194.999999999407,'2020-03-02':12942.178556189565,'2020-03-09':12676.000000003925,'2020-03-16':9839.000000000065,'2020-03-23':10872.386525901276,'2020-03-30':11594.242224694048},'shop2':{'2020-01-06':21235.830898431894,'2020-01-13':21031.1531947192,'2020-01-20':21007.500000000087,'2020-01-27':22533.000000009146,'2020-02-03':24665.31329061354,'2020-02-10':23669.18106510104,'2020-02-17':21559.90374194961,'2020-02-24':21096.769732574685,'2020-03-02':22949.18097357484,'2020-03-09':26167.931841454425,'2020-03-16':31657.999999966796,'2020-03-23':23706.24281903446,'2020-03-30':22218.329375006986},'shop3':{'2020-01-06':150580.18064739247,'2020-01-13':171580.040557476,'2020-01-20':198202.9999999497,'2020-01-27':200059.80313551775,'2020-02-03':207317.58264445866,'2020-02-10':215898.51182939706,'2020-02-17':220737.15944472587,'2020-02-24':227932.5231698131,'2020-03-02':237088.36782405066,'2020-03-09':261823.35184683453,'2020-03-16':301458.9999998379,'2020-03-23':278815.551154112,'2020-03-30':272998.0208560584}}

解决方案

您可以创建颜色列表,然后在 df.plot

中使用 colormap 参数从matplotlib.colors中的

 导入LinearSegmentedColormap#按照商店的顺序创建颜色列表颜色 = ['r','g','b']#创建自定义颜色图lscm = LinearSegmentedColormap.from_list('颜色', 颜色)# 阴谋(df.plot(figsize=(20,10),lw=2.5,linestyle='--', colormap=lscm).legend(loc = 2,prop = {'size':13}))

如果您只想为所有帧创建一个关键点,则可以将颜色压缩到列中,并使用该指令将每种颜色映射到您的列中

  colors = ['r','g','b']# 将颜色压缩到您的列colordict = dict(zip(df.columns,colors))# 从 colordict 映射任何你想要的数据框的颜色cmap = df.columns.map(colordict).values.tolist()#然后分配给lscmlscm = LinearSegmentedColormap.from_list('color', cmap)

I have a pandas dataframe, and would like to compare two line plots or "spaghetti" plots. The 2nd plot has one column removed from the dataframe.

However it gets a bit confusing when the colors rearrange between plots.

An example, weekly sales of eight shops:

(df.plot(figsize=(20,10),lw=2.5,linestyle='--')
 .legend(loc=2, prop={'size': 13}))

I can see shop1 (blue) is drowning out the others. So I decide to remove shop1, and plot it again:

# drop shop 1
df.drop('shop1',axis=1,inplace=True)

# plot again
(df.plot(figsize=(20,10),lw=2.5,linestyle='--')
 .legend(loc=2, prop={'size': 13}))

Now the colors have rearranged themselves. Shop2 was orange, but now it's green. All colors have been shifted.

Is there an easy method of preserving the colors for each shop between graphs?

I've been testing for an hour different ways to get around this. Using pop to get rid of line objects, trying to hack into the matplotlib color_cycle, I even tried to hide the line by setting values to zero with a color white.

Sample dataframe, for pd.DataFrame.from_dict():

df.to_dict()

{'shop1': {'2020-01-06': 9778.763579802846,
  '2020-01-13': 10294.040674742606,
  '2020-01-20': 10748.72889467783,
  '2020-01-27': 9995.956972783448,
  '2020-02-03': 11013.304192764444,
  '2020-02-10': 13165.999999999907,
  '2020-02-17': 11180.50000000096,
  '2020-02-24': 9194.999999999407,
  '2020-03-02': 12942.178556189565,
  '2020-03-09': 12676.000000003925,
  '2020-03-16': 9839.000000000065,
  '2020-03-23': 10872.386525901276,
  '2020-03-30': 11594.242224694048},
 'shop2': {'2020-01-06': 21235.830898431894,
  '2020-01-13': 21031.1531947192,
  '2020-01-20': 21007.500000000087,
  '2020-01-27': 22533.000000009146,
  '2020-02-03': 24665.31329061354,
  '2020-02-10': 23669.18106510104,
  '2020-02-17': 21559.90374194961,
  '2020-02-24': 21096.769732574685,
  '2020-03-02': 22949.18097357484,
  '2020-03-09': 26167.931841454425,
  '2020-03-16': 31657.999999966796,
  '2020-03-23': 23706.24281903446,
  '2020-03-30': 22218.329375006986},
 'shop3': {'2020-01-06': 150580.18064739247,
  '2020-01-13': 171580.040557476,
  '2020-01-20': 198202.9999999497,
  '2020-01-27': 200059.80313551775,
  '2020-02-03': 207317.58264445866,
  '2020-02-10': 215898.51182939706,
  '2020-02-17': 220737.15944472587,
  '2020-02-24': 227932.5231698131,
  '2020-03-02': 237088.36782405066,
  '2020-03-09': 261823.35184683453,
  '2020-03-16': 301458.9999998379,
  '2020-03-23': 278815.551154112,
  '2020-03-30': 272998.0208560584}}

解决方案

You can create a color list then use the colormap param in df.plot

from matplotlib.colors import LinearSegmentedColormap
# create a color list in the order of your shops
colors = ['r','g','b']
# create a custom color map
lscm = LinearSegmentedColormap.from_list('color', colors)

# plot 
(df.plot(figsize=(20,10),lw=2.5,linestyle='--', colormap=lscm)
 .legend(loc=2, prop={'size': 13}))

If you only want to create one key for all frames then you can zip your colors to your columns and use that dict to map each color to you columns

colors = ['r','g','b']
# zip the colors to your columns
colordict = dict(zip(df.columns, colors))
# map colors from colordict whatever dataframe you want
cmap = df.columns.map(colordict).values.tolist()
# then assign to lscm
lscm = LinearSegmentedColormap.from_list('color', cmap)

这篇关于不同图之间的线是否保持相同的颜色顺序?我删除了一行,颜色现在重新排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆