简体   繁体   中英

2D Heatmap using python for already binned data

I would like to obtain a 2D Heatmap (using python) for a dataset that is already binned such that I have left and right edges for x and y and then the data I am interested (that I would like colormapped) as a function of x and y.

I give an example below of what my dataset looks like:

x_min x_max y_min y_max Data
1 2 0 0.1 10
2 3 0 0.1 13
3 4 0 0.1 12
4 5 0 0.1 20
1 2 0.1 0.2 9
2 3 0.1 0.2 17
3 4 0.1 0.2 22
4 5 0.1 0.2 30

I would like to plot a 2D Heatmap like this below: 2D Heatmap

How would that be possible so that I get a heat map that would have for Y axis from 0 to 0.2 and for X axis from 1 to 5, and a colorbar dependent on 'data'?

Thank you!

plt.imshow() could be used as follows:

import matplotlib.pyplot as plt
import pandas as pd
from io import StringIO

data_str = '''
x_min   x_max   y_min   y_max   Data
1   2   0   0.1 10
2   3   0   0.1 13
3   4   0   0.1 12
4   5   0   0.1 20
1   2   0.1 0.2 9
2   3   0.1 0.2 17
3   4   0.1 0.2 22
4   5   0.1 0.2 30'''
df = pd.read_csv(StringIO(data_str), delim_whitespace=True)

plt.imshow(df['Data'].to_numpy().reshape(2, 4), origin='lower', extent=[1, 5, 0, 0.2], aspect='auto', cmap='plasma')
plt.colorbar()
plt.xticks(range(1, 6))
plt.yticks([0, 0.1, 0.2])
for row in df.itertuples(index=False):
    plt.text((row.x_min + row.x_max) / 2, (row.y_min + row.y_max) / 2, f"{row.Data:.0f}",
             color='navy' if row.Data > 22 else 'yellow', size=20, ha='center', va='center')
plt.show()

示例热图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM