python 画出热图
时间: 2023-10-14 16:07:48 浏览: 217
要在Python中绘制热图,可以使用不同的库和方法,下面是几个例子:
方法一:
```python
from matplotlib import pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
# 练习的数据
data = np.arange(25).reshape(5, 5)
data = pd.DataFrame(data)
# 绘制热图
plot = sns.heatmap(data)
plt.show()
```
方法二:
```python
from matplotlib import pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
from matplotlib import colorbar
# 练习的数据
data = np.arange(-18, 18).reshape(6, 6)
data = pd.DataFrame(data)
# 绘制热图
tick_ = np.arange(-20, 20, 5).astype(float)
dict_ = {'orientation': 'vertical', "label": "color \ scale", "drawedges": True,
"ticklocation": "right", "extend": "min", "filled": True, "alpha": 0.8,
"cmap": "cmap", "ticks": tick_, "spacing": 'proportional'}
# 绘制添加数值和线条的热图
cmap = sns.heatmap(data, linewidths=0.8, annot=True, fmt="d")
plt.xlabel("X", size=20)
plt.ylabel("Y", size=20, rotation=0)
plt.title("heatmap", size=20)
# 调整色带的标签
cbar = cmap.collections[0].colorbar
cbar.ax.tick_params(labelsize=20, labelcolor="blue")
cbar.ax.set_ylabel(ylabel="color scale", size=20, color="red", loc="center")
plt.show()
```
方法三:
```python
from folium.plugins import HeatMap, HeatMapWithTime
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
# 导入所需的库
# 创建一个示例数据
data = pd.DataFrame(np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4],
columns=['lat', 'lon'])
# 创建一个地图对象
m = folium.Map([37.76, -122.4], zoom_start=13)
# 创建热图
heat_map = HeatMap(data.values, min_opacity=0.3, radius=15)
heat_map.add_to(m)
# 保存地图
m.save('heatmap.html')
```
希望这些例子对你有帮助!如果你有任何相关问题,请随时提问。
相关问题:
1. 如何将热图保存为图片文件?
2. 如何修改热图的颜色映射?
3. 如何在热图上显示数值标签?
阅读全文