0

I'm having a lot of trouble trying to give each line in a chart I've produced to have a unique color. Matplotlib.pyplot function plt.errorbar() seems to cycle through 10 colors and repeats. My plot is generated using a for loop with a static x from a dataframe index and multiple y's from the dataframe columns:

fig = plt.figure()
for col in df_avg_diff.columns:
        plt.errorbar(x=df_avg_diff.index, y=df_avg_diff[col])
plt.show()

How would I alter this code to include color variation for every y? Thanks!

1 Answer 1

1

This is not an issue of the errorbar function, but rather the default color cycle (or, more generally, property cycle) of matplotlib. Check out this link for an example.

Probably the easiest way to get a different color for every line is to define them yourself, using any of the predefined colormaps - e.g. jet:

colors = plt.cm.jet(np.linspace(0, 1, n_columns))

This partitions the colormap range into the number of colors you need. Then you can either change the default property cycler to the newly created one or simply pass the color code for every line:

for i, col in enumerate(df_avg_diff.columns):
        plt.errorbar(x=df_avg_diff.index, y=df_avg_diff[col], color=colors[i])
Sign up to request clarification or add additional context in comments.

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.