简体   繁体   中英

matplotlib: scatter and colorbar values

I have data:

values = np.array( [0,1,2,3,4,5] )

I want to plot values as pyplot.scatter with different sizes and colours. Also, I want to resize the dots so the smallest dots are well visible.

If I do:

sc = pyplot.scatter ( pos_x, pos_y, values )
pyplot.colorbar( sc )

then I get the proper values and colors, but the smallest dots are too small . Now if I do:

values_scaled = (values * 2 + 1)
sc = pyplot.scatter ( pos_x, pos_y, values_scaled )
pyplot.colorbar( sc )

then the dots have acceptable sizes (although a bit fake) but the colorbar legend starts at 1 and ends at 11, which I don't want. I want to scale just sizes of the dots, but not the values assigned there. How to do that?

The matplotlib.pyplot.scatter functions takes an optional keyword argument s , which is the "size in points^2. It is a scalar or an array of the same length as x and y" . So just pass your values_scaled array as the value of this argument and leave the colour data (the optional c keyword argument) as values , ie:

sc = pyplot.scatter(pos_x, pos_y, c=values, s=values_scaled)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM