0

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.

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

enter image description here

Question: How can I access the default color list of my plot.line to use it in plot.scatter? Or is there another, easier way to highlight the data which constitutes the lines?

2
  • pass c='r' into .plot.scatter? Commented Jun 19, 2019 at 13:56
  • This will change everything from all-blue in all-red. I need the same colours as given by plot.line default to distinguish my cases. regards Commented Jun 19, 2019 at 13:58

2 Answers 2

2

There's a marker option in plot.line:

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:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

The marker is not shown in the first two legend entries?
@Sheldore Interestingly, no. They only show with a call of plt.legend(). A bug maybe?
2

IIUC, you can try, although @QuangHoang is the better solution:

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: enter image description here

2 Comments

Good to know how to access the color sequence. Although, the color=color[i-1] only works if df.id.unique() == (1,2,3) like this special case.
Yes, That is true, it is better to use enumerate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.