0

I have a script that plots out a heat map using matplotlib. Range of X-axis value = (-180 to +180) and Y-axis value =(0 to 180). The 2D heatmap colours areas in Rainbow according to the number of points occurring in a specified area in the x-y graph (defined by the 'bins' - see code below). In this case, x = values_Rot and y = values_Tilt (see below for code).

As of now, this script colours the 2D-heatmap on a log_base10 scale. However, the range of my data is small and I am thinking of changing the base from 10 to base 2.

Is there a way to colour the heatmap by changing the base from 10 to 2 in this code?

Also, I find that the image which has popped up after running this script clearly shows the heatmap. However, when I see the PNG image that has been saved, I see it is a blank image (pure white image) with nothing on it. Is there a way to save the figure - am I missing something while importing?

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

 values_Rot = []
values_Tilt = []
values_Psi = []

for line in data:
    try:
        values_Rot.append(float(line.split()[rot_number]))
        values_Tilt.append(float(line.split()[tilt_number]))
        values_Psi.append(float(line.split()[psi_number]))

    except:
        print ('This line didnt work, it may just be a blank space. The line is:' + line)


# Change the values here if you want to plot something else, such as psi. 
# You can also change how the data is binned here.
plt.hist2d(values_Rot, values_Tilt, norm=mpl.colors.LogNorm(), bins=100,)
plt.set_cmap('jet')
plt.colorbar()
plt.show()


plt.savefig('name_of_output.png')
Minder
  • 1
  • Did you read my answer to your [previous question](https://stackoverflow.com/questions/64260868/change-colour-scheme-label-to-log-scale-without-changing-the-axis-in-matplotlib/64261478#64261478)? One of the things it explains, is that you should call `savefig` before `plt.show()`. Please don't mistake 'jet' for a rainbow. 'jet' can be very harmful. Note that log2 and log10 scale exactly the same. The answer to the previous question explains how to change the ticks of the colorbar and links to a relevant github discussion. – JohanC Oct 08 '20 at 16:36
  • I did read your previous post has modified the code heavily. Sorry - I am still trying to change the base form base 10 to base 2 without modifying this code heavily, like your previous answer. And Log-base 2 and log10 are very different. One is them is in factor of 10s and the other is in factor of 2. I am not sure what you mean by they are the same. And log-base2 scale is important for me because my heattmap goes to a maximum of 9.2 (range of numbers is 0-9.2, including decimals). Cheers – Minder Oct 09 '20 at 10:51
  • Well, log10 and log2 are exactly the same transformation for the color. Only the ticks shown in the colorbar are at different places. Also note that in a not-normalized histogram all values are counts and thus integers. If the colorbar goes till 9.2, there will only be 10 possible values. – JohanC Oct 09 '20 at 15:28

0 Answers0