ThunderFlash
ThunderFlash

Reputation: 522

Setting colour scale to log in a contour plot

I have an array A which I have plotted in a contour plot using X and Y as coordinate axes,

plt.contourf(X,Y,A)

Contour plot of A

Problem is, the values in A vary from 1 to a very large number such that the color scale doesn't show a plot. When I plot log(A), I get the following contour,

Contour plot of log(A)

which is what I'm looking for. But I want to be able to view the values of the array A, instead of log(A), when I hover my cursor over a certain (X,Y) point. I already got an answer for how to do that, but how would I go about doing it while my colour scale remains log? Basically what I'm trying to do is to make the color scale follow a log pattern, but not the array values themselves.

Thanks a lot!

Upvotes: 2

Views: 2411

Answers (2)

Julien Spronck
Julien Spronck

Reputation: 15433

You can do this:

from matplotlib import colors
plt.contourf(X, Y, A, norm=colors.LogNorm())
plt.colorbar()
plt.show()

or

from matplotlib import ticker
plt.contourf(X, Y, A, locator=ticker.LogLocator())
plt.colorbar()
plt.show()

Upvotes: 4

jotasi
jotasi

Reputation: 5177

A similar question was already asked for log-scaling the colors in a scatter plot: A logarithmic colorbar in matplotlib scatter plot

As is it was indicated there, there is an article in matplotlibs documentation that describes norms of colormaps: http://matplotlib.org/devdocs/users/colormapnorms.html

Essentially, you can set the norm of your contourplot by adding the keyword , norm=matplotlib.colors.LogNorm()

Upvotes: 1

Related Questions