Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A logarithmic colorbar in matplotlib scatter plot

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.

current plot

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.

like image 342
Cokes Avatar asked Jun 19 '13 20:06

Cokes


People also ask

What is logarithmic scale in Matplotlib?

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.

How do you normalize a Colorbar in Python?

norm = normi; #mpl. colors. Normalize(vmin=-80, vmax=20); plt. axis([1, 1000, -400, 400]);

How do I scale a Colorbar in Matplotlib?

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.

Can you use Matplotlib to create a colored scatterplot?

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.


1 Answers

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
like image 183
tacaswell Avatar answered Oct 02 '22 23:10

tacaswell