aloha
aloha

Reputation: 4784

errorbars & colorbars python

I want to plot a scatter plot, that has a colorbar, and the data has errorbars. Here's my code:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(123)

x = np.linspace(0.1, 100, 10)
y = np.linspace(6, 18, 10)
yerr = np.random.random(10)
z = np.linspace(0, 10, 10)

plt.scatter(x, y, s = 20, c = z)
plt.colorbar()
plt.errorbar(x, y, yerr = yerr, fmt = '.')

Here's the plot: enter image description here

However, the errorbars are drawn in blue. I want to draw them according to the colorbar, how can I do this? For example, if a point has a value of 10 the errorbar should be drawn in red.

EDIT

Following the answer suggested below, I wrote:

cmap = matplotlib.cm.get_cmap('jet')
norm = matplotlib.colors.Normalize(vmin=min(y), vmax=max(y))

plt.scatter(x, y, s = 20, c=z, cmap=cmap)
plt.colorbar()
plt.errorbar(x, y, yerr = yerr, fmt = '.', c=cmap(norm(y)))

But still, it didn't work. Any suggestions?

I am still interested to get an answer to this question. Anyone?

Upvotes: 1

Views: 4409

Answers (1)

xnx
xnx

Reputation: 25548

You can color your errorbars with a colormap, though you probably want to normalize the values you send it:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib

np.random.seed(123)

x = np.linspace(0.1, 100, 10)
y = np.linspace(6, 18, 10)
yerr = np.random.random(10)
z = np.linspace(0, 10, 10)

cmap = matplotlib.cm.get_cmap('jet')
norm = matplotlib.colors.Normalize(vmin=min(y), vmax=max(y))

plt.scatter(x, y, s = 20, c=z, cmap=cmap)
plt.colorbar()
plt.errorbar(x, y, yerr = yerr, fmt = '.', c=cmap(norm(y)))

Upvotes: 2

Related Questions