[英]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.我想为已经分箱的数据集获取 2D 热图(使用 python),这样我有 x 和 y 的左右边缘,然后是我感兴趣的数据(我想要颜色映射)作为 x 的 function和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我想 plot 如下所示的 2D 热图: 2D 热图
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'?这怎么可能让我得到一个热量 map, Y 轴从 0 到 0.2, X 轴从 1 到 5,以及一个取决于“数据”的颜色条?
Thank you!谢谢!
plt.imshow()
could be used as follows: plt.imshow()
可以按如下方式使用:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.