Matplotlib: Scatter plot in a loop over set of arrays with consistent scatter point size and color bar

Matplotlib: Scatter plot in a loop over set of arrays with consistent scatter point size and color bar

Problem Description:

I am trying to generate a scatter plot using dataframe series x & y and the size of the scatter data point using dataframe series z.

I should mention that I iterate through a set of each x,y, and z arrays and add the color plot outside the loop.

I see that the scatter sizes and color bar are generated at each iteration therefore scatter sizes are not consistent with all data points in the plot and also with the colorbar at the end. How do I solve this?

fig, ax = plt.subplots()
for x, y, z in arrays_of_xyz:
     splot = ax.scatter(x.to_numpy(), y.to_numpy(),  marker= 'o', s = z.to_numpy(), cmap ='viridis_r', c = z.to_numpy())

fig.tight_layout()
plt.colorbar(splot)
plt.show()

Gautham

Solution – 1

Can’t see in which way the sizes in the plot are inconsistent.
The colorbar can be inconsistent if you do not enforce consistent vmin and vmax when calling scatter.

Can you please try with the following code and tell more about inconsistencies you got:

import numpy as np
import matplotlib.pyplot as plt

num_sets = 3
colors = ("red", "green", "blue")
num_pts_per_set = 20

xs = np.random.randn(num_sets, num_pts_per_set)
ys = np.random.randn(num_sets, num_pts_per_set)
zs = (
    np.random.rand(num_sets, num_pts_per_set)
    * np.arange(1, num_sets + 1).reshape(-1, 1)
    * 30
)
zmin = zs.min()
zmax = zs.max()

fig, (ax1, ax2) = plt.subplots(ncols=2)

ax1.set_title("Sizes according to znColors according to set #")
for i, (x, y, z, clr) in enumerate(zip(xs, ys, zs, colors)):
    ax1.scatter(x, y, marker="o", s=z, c=clr, label=f"Set #{i}")
ax1.legend()

ax2.set_title("Facecolors according to znSizes according to set #")
for i, (x, y, z, clr) in enumerate(zip(xs, ys, zs, colors)):
    splot = ax2.scatter(x, y, marker="o", c=z, edgecolors=clr, s=(i+1)*30, vmin=zmin, vmax=zmax, label=f"Set #{i}")
ax2.legend()
fig.colorbar(splot)

plt.show()

scatter plot

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject