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
CodePudding user response:
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 z\nColors 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 z\nSizes 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()