I would like to make the colors of the points on the scatter plot correspond to the value of the void fraction, but on a logarithmic scale to amplify differences. I did this, but now when I do plt.colorbar(), it displays the log of the void fraction, when I really want the actual void fraction. How can I make a log scale on the colorbar with the appropriate labels of the void fraction, which belongs to [0.00001,1]?
Here is an image of the plot I have now, but the void fraction colorbar is not appropriately labeled to correspond to the true void fraction, instead of the log of it.
fig = plt.figure() plt.scatter(x,y,edgecolors='none',s=marker_size,c=np.log(void_fraction)) plt.colorbar() plt.title('Colorbar: void fraction')
Thanks for your help.
The logarithmic scale is useful for plotting data that includes very small numbers and very large numbers because the scale plots the data so you can see all the numbers easily, without the small numbers squeezed too closely.
norm = normi; #mpl. colors. Normalize(vmin=-80, vmax=20); plt. axis([1, 1000, -400, 400]);
Use the matpltolib. pyplot. clim() Function to Set the Range of Colorbar in Matplotlib. The clim() function can be used to control the range of the colorbar by setting the color limits of the plot, which are used for scaling.
Matplotlib scatter plot color label To create a scatter plot, we use the scatter() method. To add a legend to a plot, we use the legend() method. To set the color of the legend, we pass facecolor parameter to the method.
There is now a section of the documentation describing how color mapping and normalization works
The way that matplotlib
does color mapping is in two steps, first a Normalize
function (wrapped up by the sub-classes of matplotlib.colors.Normalize
) which maps the data you hand in to [0, 1]
. The second step maps values in [0,1]
-> RGBA space.
You just need to use the LogNorm
normalization class, passed in with the norm
kwarg.
plt.scatter(x,y,edgecolors='none',s=marker_size,c=void_fraction, norm=matplotlib.colors.LogNorm())
When you want to scale/tweak data for plotting, it is better to let matplotlib
do the transformations than to do it your self.
Normalize
doc LogNorm
doc matplotlib.color
doc If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With