Home > Mobile >  How do I represent the specific color in two color matplotlib graph numerically?
How do I represent the specific color in two color matplotlib graph numerically?

Time:12-15

As a part of my project, I generated a matplotlib graph. This graph is a two color graph plotted from a numpy array of shape (1,128,128,1) as shown

Matplotlib graph

The yellow color region in the graph is my required area out of the whole region represented by purple color.

Is there any way to represent the yellow colored region in the graph numerically. Is it possible to get the

  1. Ratio of yellow colored region to the purple colored region
  2. number of yellow colore dpixels in the graph compared to the purple colored region

Please help me.

CodePudding user response:

You didn't provide the code so I give just an example. Apply it to your array.

All you need to do here is to work with original array you are plotting and use

c = np.count_nonzero(arr == yellow_value)

This will count all elements with yellow value in your array.

Then you can count ratio:

ratio = c/(128*128)

128*128 stands for total number of pixels in the array. You can parametrize this via array dimensions.

CodePudding user response:

There are two ways you can go about this problem:
1- In case you have the array itself, you can just threshold the values, calculate the counts of low and high values and finally calculate the ratio.

By the way, in default, the yellow color represents high values and purple color represents low values when you plot 2d graphs with 'matplotlib' library.

Code:

import numpy as np
... 
array = make_arbitrary_array()

# Here, we are selecting the midrange as the threshold value,
#  you can change this as you see fit
threshold = (array.min()   array.max()) / 2

total_value_count = np.prod(array.shape)
high_value_count  = np.sum(array > threshold) 
low_value_count   = total_value_count - high_value_count

high_low_ratio = (high_value_count / low_value_count)
high_all_ratio = (high_value_count / total_value_count)
low_all_ratio  = (low_value_count  / total_value_count)

print(f"Ratio of yellow region over purple region {high_low_ratio*100:.3f}%")
print(f"Ratio of yellow region over total {high_all_ratio*100:.3f}%")
print(f"Ratio of purple region over total {low_all_ratio*100:.3f}%")

Out:

Ratio of yellow region over purple region 22.805%
Ratio of yellow region over total 18.570%
Ratio of purple region over total 81.430%

Plotted Image: Image of the Plot

2- If you do not have access to the numpy array, but only have the access to the image of the plot; you need to load the image to Python, convert pixels to an array and use the same code as in (1) to calculate ratios from the array.

There may be some problems with this approach, for example, the plot may be interpolated badly (such as in your case if you have only two values in the array, there are pixels with green color, neither yellow nor purple). Also, the image you have should have been saved as a file, which in turn means it may be compressed in a lossy manner (such as .jpeg files). It is better if you use lossless compressions such as .png files for your case.

I am using OpenCV to load the image which you can use pip to install to your environment. To install OpenCV run the following line in your shell: pip install opencv-python

Image which is in the same folder as your code: plot_image.png

Code:

import numpy as np
import cv2

# reading the values in grayscale to make it easier to process
img = cv2.imread("plot_image.png", cv2.IMREAD_GRAYSCALE)
array = np.array(img.data)

... # codes in the part (1)

Out:

Ratio of yellow region over purple region 22.805%
Ratio of yellow region over total 18.570%
Ratio of purple region over total 81.430%

We get the same outputs as before with this approach as well.

  • Related