Reputation: 47
I am trying to match the color of the errorbars with the color of the data points using the code below but I am getting the following error
raise ValueError("RGBA sequence should have length 3 or 4")
ValueError: RGBA sequence should have length 3 or 4
What I am doing wrong? Any advice would be gratefully received.
Data format:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.style
import matplotlib.font_manager
data = pd.read_csv('error_data')
size = data['size']
x = data['x']
y = data['y']
error = data['error']
plot_xy = plt.scatter(x, y ,s=20,c=size, alpha=0.5)
colors = plt.colorbar(plot_xy)
size_colors = mpl.colors.to_rgba(size)
for x, y, e, color in zip(x, y, error ,colors):
plt.errorbar(x, y, e, lw=1, capsize=3, color=color, alpha=0.5)
plt.show()
Upvotes: 2
Views: 1102
Reputation: 47
Thank you for the help. I managed to get it working from adapting the code (shown below) from this question Colormap for errorbars in x-y scatter plot using matplotlib
import matplotlib.pyplot as plt
import numpy as np
#data
time = np.arange(100.)
signal = time**2
error = np.ones(len(time))*1000
#create a scatter plot
sc = plt.scatter(time,signal,s=0,c=time)
#create colorbar according to the scatter plot
clb = plt.colorbar(sc)
#convert time to a color tuple using the colormap used for scatter
import matplotlib
import matplotlib.cm as cm
norm = matplotlib.colors.Normalize(vmin=min(signal), vmax=max(signal), clip=True)
mapper = cm.ScalarMappable(norm=norm, cmap='viridis')
time_color = np.array([(mapper.to_rgba(v)) for v in signal])
#loop over each data point to plot
for x, y, e, color in zip(time, signal, error, time_color):
plt.plot(x, y, 'o', color=color)
plt.errorbar(x, y, e, lw=1, capsize=3, color=color)
Upvotes: 1