Home > Software engineering >  Plot scatterplots with errors in a colorbar
Plot scatterplots with errors in a colorbar

Time:12-19

I have a dataset with

x = [1,2,3,4,5]
y = [2,3,4,5,6]
z = [4,5,6,7,8]
x_err = [0.1,0.2,0.3,0.2]
y_err = [0.1,0.2,0.3,0.2]]

I want to plot (x,y,x_err,y_err) with colorbar of z (Want to see x&y variations with respect to z). I cannot plot errors in plt.scatter() and I am not able to plot colorbars in plt.errorbar().

Need help with this.

CodePudding user response:

Try it

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

scatter = ax.scatter(x, y, c=z, s=50, cmap='viridis')
errorbar = ax.errorbar(x, y, xerr=x_err, yerr=y_err, fmt='none', c='black')

cbar = fig.colorbar(scatter)
cbar.set_label('z')

plt.show()

CodePudding user response:

You can use an importable matplotlib library to help you here is a example that your variables can be worked into;

import matplotlib.pyplot as plt
import numpy as np

plt.style.use('_mpl-gallery')

# make the data
np.random.seed(3)
x = 4   np.random.normal(0, 2, 24)
y = 4   np.random.normal(0, 2, len(x))
# size and color:
sizes = np.random.uniform(15, 80, len(x))
colors = np.random.uniform(15, 80, len(x))

# plot
fig, ax = plt.subplots()

ax.scatter(x, y, s=sizes, c=colors, vmin=0, vmax=100)

ax.set(xlim=(0, 8), xticks=np.arange(1, 8),
   ylim=(0, 8), yticks=np.arange(1, 8))

plt.show()

Use matplotlib to help you

  • Related