pythonmatplotlibrgbacolorbarimshow

Show "Heat Map" image with alpha values - Matplotlib / Python


I'm trying to plot some data to analyze them.

My data is defined as below:

class Data(object):
    def __init__(self, rows=200, cols=300):
        """
        The Data constructor
        """
        # The data grid
        self.cols = cols
        self.rows = rows
        # The 2D data structure
        self.data = numpy.zeros((rows, cols), float)

At first, I had this method:

  def generate_data_heat_map(data, x_axis_label, y_axis_label, plot_title, file_path):
    plt.figure()
    plt.title(plot_title)
    fig = plt.imshow(data.data, extent=[0, data.cols, data.rows, 0])
    plt.xlabel(x_axis_label)
    plt.ylabel(y_axis_label)
    plt.colorbar(fig)
    plt.savefig(file_path + '.png')
    plt.close()

This gives me something as a heat map image (second figure), 'cause I'm passing to it an MxN [luminance (grayscale, float array only)]. And don't know why this doesn't generate a grayscale image, but so far I didn't worry about it 'cause that is the result I wanted.

After some more calculation, I had this method to visualize my data, using the data_property as RGB and data_uncertaity as alpha:

def generate_data_uncertainty_heat_map(data_property, data_uncertainty, x_axis_label, y_axis_label, plot_title, file_path):
    plt.figure()
    uncertainty = numpy.zeros((data_property.rows, data_property.cols, 4))
    uncertainty[..., :3] = data_property.data[..., numpy.newaxis]
    uncertainty[..., 3] = data_uncertainty.data
    plt.title(plot_title)
    fig = plt.imshow(uncertainty.data, extent=[0, data_property.cols, data_property.rows, 0])
    plt.xlabel(x_axis_label)
    plt.ylabel(y_axis_label)
    plt.colorbar(fig)
    plt.savefig(file_path + '.png')
    plt.close()

But, of course, this give me a grayscale image with alpha values, since I am repeating the same values for R, G and B. But what I really would like to have was the first method result (colored) with some alpha values calculated as uncertainty about the data.

enter image description here

I've noticed that my color bar has nothing about my data too (it's in RGB, I can't use it to analyze my data)

I don't know how to achieve the result that I want, which is to a have a "heat map" plot with merged the alpha values defined with my uncertainty_data and a color bar representing this uncertainty. Like merging this two images above:

This as my color: Figure 2

This as my alpha: enter image description here

With the conversion presented by @BlazBratanic, I guess I can see a little bit of color (not sure about it), but its far of what I was expecting.

enter image description here

All my values is between 0.0 and 1.0.

Thank you in advance.


Solution

  • Use Matplotlib cm module to map your grayscale to color values. If i remember correctly "jet" is the default colormap. So you would do something like:

    uncertainty = plt.cm.jet(data_property.data)
    uncertainty[..., 3] = data_uncertainty.data