Home > OS >  Customize colors in error plots with Plotlib
Customize colors in error plots with Plotlib

Time:06-10

I would like to change the colors in this error plot done with Plotlib in order to customize the choice. I would have the errors plots at label 0 (data1 and data3) with a different color (let's say green) with respect to the ones on the right (label 1, so data 2 and data4 and let's say red). Moreover, I would like to differentiate the two series (blue and orange with different shades of the colors). I tried different methods but did not manage to customize the plot.

Final result

import matplotlib.pyplot as plt

# create sample data
N = 2
data_1 = {
    'x': list(range(N)),
    'y': [2,3],
    'yerr': [0.2,0.2]}
data_2 = {
    'x': list(range(N)),
    'y': [4,5],
    'yerr': [0.4,0.2]}

data_3 = {
    'x': list(range(N)),
    'y': [6,10],
    'yerr': [0.3,0.7]}
data_4 = {
    'x': list(range(N)),
    'y': [8.1,10.6],
    'yerr': [0.1,0.8]}

# plot

# only errorbar
plt.subplot(121)
for data in [data_1, data_2]:
    plt.errorbar(**data, fmt='o')
plt.xlim([-1, 2])
plt.xticks([0, 1])

    
plt.subplot(122)
for data in [data_3, data_4]:
    plt.errorbar(**data, fmt='o')
plt.xlim([-1, 2])
plt.xticks([0, 1])

CodePudding user response:

When plotting with plot or errorbar, a sequence of points all get the same color. You can specify it with c=<some color>. In your case, you want this sequence of points to have different colors. This can be done in two ways: 1) Iterate through the sequence of points and select a color or 2) use plt.scatter and provide a sequence of points. The latter method, however, doesn't have the yerr option. One solution is to plot twice, one for the error bars, another for the markers. See this example, using the same data you provided:

plt.subplot(121)
for data in [data_1, data_2]:
    plt.errorbar(**data, linestyle="None", c='k') # Only errorbars
    plt.scatter(data['x'], data['y'], c=['green', 'red']) # Only markers
plt.xlim([-1, 2])
plt.xticks([0, 1])

plt.subplot(122)
for data in [data_3, data_4]:
    plt.errorbar(**data, linestyle="None", c='k')
    plt.scatter(data['x'], data['y'], c=['lime', 'lightcoral'])
plt.xlim([-1, 2])
plt.xticks([0, 1])

Yields this plot:

enter image description here

Now it's a matter of choosing the colors you want.


Answering the comment: To get the colors to vary, you'll need to supply them directly or find a way of mapping some unique information (say, a field "name" of a row in a dataframe). Here's two methods of supplying them directly:

plt.errorbar(**data_1, linestyle="None", c='k')
plt.scatter(data_1['x'], data_1['y'], c=['green', 'red'])
plt.errorbar(**data_2, linestyle="None", c='k')
plt.scatter(data_2['x'], data_2['y'], c=['blue', 'purple'])

plt.xlim([-1, 2])
plt.xticks([0, 1])

plt.subplot(122)
for (data, colors) in [(data_3, ['lime', 'lightcoral']), (data_4, ['cornflowerblue', 'magenta' ])]:
    plt.errorbar(**data, linestyle="None", c='k')
    plt.scatter(data['x'], data['y'], c=colors)
plt.xlim([-1, 2])
plt.xticks([0, 1])

and the resulting graph:

enter image description here

  • Related