简体   繁体   English

如何以相同颜色(matplotlib)将数据点添加到线图中?

[英]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? 还是有另一种更容易的方式来突出显示构成线条的数据?

There's a marker option in plot.line : plot.line有一个marker选项:

fig, ax = plt.subplots()

for i in df.id.unique():
    df[df['id']==i].plot.line(x='year', 
                              y='money', 
                              marker='o', # this add the data points on the line, with the same color 
                              ax=ax, 
                              label='id = %s'%i)

Output: 输出:

在此处输入图片说明

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))  

Output: 输出: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python 2.7 matplotlib pylab:我的情节是空的。 如何使数据点显示在图中? - python 2.7 matplotlib pylab : My plot is empty. How can I make the data points show up in the plot? 如何使用matplotlib在同一行中绘制多个图形? - How can I plot multiple figure in the same line with matplotlib? 如何在 matplotlib 上的散点图中添加图例(点根据 0 和 1 的数组进行颜色编码)? - How do I add a legend to a scatter plot on matplotlib (the points are colour coded according to an array of 0s and 1s)? 如何以不同的颜色绘制一条线以继续另一个情节? Matplotlib - How to plot a line in a different colour to continue another plot? Matplotlib 如何使密集的 plot 上的所有点在 matplotlib 中可见? - How can I make all the points on a dense plot visible in matplotlib? 平滑线 matplotlib:如何用 5 个样本点平滑线? - smooth line matplotlib: How can i smooth line with 5 points of sample? 我怎样才能在另一条线 plot 的顶部 plot 特定点? - How can I plot specific points on top of another line plot? 如何在 matplotlib 中编辑我的图例的文本颜色? - How can I edit the text colour of my legend in matplotlib? 为什么我不能在我的图中添加散点? - Why can't I add scatter points to my plot? 如何在我的情节中为`Nan`s 设置特殊颜色? - How can I set a special colour for `Nan`s in my plot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM