2

I have a whole bunch of columns in a dataframe that I'm plotting like this:

df.xs('Mean', level=1).cumsum().plot(cmap='winter').

This plots the cumulative sum of the means over time for all of my columns. (level 0 multiindex is time) However, I'd like to highlight a specific column in the plot with a different colour, but am unsure how to go about it. Something like this:

df.xs('Mean', level=1).cumsum().plot(cmap='winter', color={'my_specific_column':'red'}).

which will plot all of the other columns along the 'winter' cmap spectrum (teal to blue), except for 'my_specific_column', which will be red.

Any ideas?

2 Answers 2

1

I think the simple option is to plot twice

to_draw = df.xs('Mean', level=1).cumsum()

fig, ax = plt.subplots()

to_draw.drop('my_specific_column', axis=1).plot(cmap='winter', ax=ax)
to_draw['my_specific_column'].plot(color='red', ax=ax, label='my_specific_column')

ax.legend()
Sign up to request clarification or add additional context in comments.

Comments

1

This is another solution. Firstly we get the colors from the colormap, and set them to each of the columns we want to plot. We update the dictionary with forcing the color we want. And finally, we set a map to be applied to the plot.

import matplotlib
cmap = matplotlib.cm.get_cmap('winter')

# get colors for each of the columns
columns = df.columns.values
colors = [cmap(i) for i in range(1, len(columns)+1)]

# build a dictionary with them
COLORS_DICT = dict(zip(columns, colors))

# update with the column we want to change
COLORS_DICT.update({"my_specific_column": 'red'})
COLORS_MAP = list(map(lambda x : COLORS_DICT[x], COLORS_DICT))
df[columns].plot(color=COLORS_MAP)

Comments

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.