Home > Net >  Errorbar plot with different color on each marker
Errorbar plot with different color on each marker

Time:09-13

I can create an errorbar plot with:

import  matplotlib.pyplot as plt
import pandas as pd

data = {'x': [194.33, 281.0, 317.5, 118.66666666666667, 143.0], 
        'y': [292.83, 284.45, 302.47, 178.8, 165.81], 
        'error':[191.83094965214667, 188.15999999999997, 170.51999999999998, 35.951147099609756, 27.439999999999998],
        'color': ['yellow','red','red','yellow','red']}

#         x       y       error   color
#194.330000  292.83  191.830950  yellow
#281.000000  284.45  188.160000     red
#317.500000  302.47  170.520000     red
#118.666667  178.80   35.951147  yellow
#143.000000  165.81   27.440000     red

df = pd.DataFrame(data)
fig, ax = plt.subplots()
ax.errorbar(x=df['x'], y=df['y'], yerr=df['error'], fmt='o', ecolor='black', elinewidth=1, capsize=5, c='blue')

enter image description here

But I want to color each each observation (blue dot) using my color column, but this doesnt work. Is there some way?

ax.errorbar(x=df['x'], y=df['y'], yerr=df['error'], fmt='o', ecolor='black', elinewidth=1, capsize=5, c=df['color'])
ValueError: RGBA sequence should have length 3 or 4

CodePudding user response:

Unfortunately, this doesn't work (see a discussion here on github).

You can solve this by using a loop, and plotting them one by one:

for x, y, err, colors in zip(df['x'], df['y'], df['error'], df['color']):
    ax.errorbar(x=x, y=y, yerr=err, fmt='o', ecolor='black', elinewidth=1, capsize=5, color = colors)
  • Related