Henri Plana
Henri Plana

Reputation: 67

Plot an matrix with python containing X, Y, Radius, Intensity

I have an array containing the following columns: X(position), Y(position), R(radius) and Intensity(inside the radius) I would like to plot an (X,Y) map containing circle with different radii and each circle will have a color according to the intensity column. I saw this king of plot around. In order for the plot to be lisible, I plan to separate the Intensity in say, 4 or 5 bins in order to have 4 or 5 colors only. I was thinking to use "plt.imshow", but I do not want a square pixel, I want circles with with different radii. If someone has some ideas, I'll appreciate .. Thanks

Upvotes: 1

Views: 141

Answers (1)

pakpe
pakpe

Reputation: 5479

Normally you would use a scatter plot for this type of data. However, I have found that if I plot each point as an annotation mark, I have the most control over size and color of the marker, which is what you need for this problem. Let's say you have the following sample data in a csv file:

x,y,radius,intensity
1,4,3,10
2,4,4,20
4,2,5,30
6,8,3,40
8,10,2,50
9,4,6,50
3,2,3,20

You can plot it like this:

import pandas as pd
from matplotlib import pyplot as plt

df = pd.read_csv('sample2.csv')

intensity_range = (df.intensity.max()-df.intensity.min())
colors = ['red','orange', 'yellow', 'green', 'blue']

for _,row in df.iterrows():
    plt.annotate(text=' ', xy= (row['x'],row['y']), size= row['radius'], bbox={"boxstyle":"circle","color":colors[int(row['intensity']*4/intensity_range)-1]})

plt.xlim(df.x.min() - 1, df.x.max() + 1)
plt.ylim(df.y.min() - 1, df.y.max() + 1)
plt.show()

enter image description here

You can use the same annotation strategy to add a customized color bar as shown below. Play with the constants in the code below to get it to look the way you want.

for i, color in enumerate(colors):
    plt.annotate(text= str(i* intensity_range//4 + df.intensity.min()).ljust(8, ' '), xy=(df.x.mean() + i, df.y.max()+ 1), size=(10), bbox={"boxstyle": "square", "color": color})

enter image description here

Upvotes: 2

Related Questions