700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python echarts城市热力图_python如何画热力图?

python echarts城市热力图_python如何画热力图?

时间:2021-02-06 17:03:42

相关推荐

python echarts城市热力图_python如何画热力图?

其实没有那么麻烦,就使用matplotlib就可以了,plt.matshow(data)然后再加一个color bar基本上完事.官方案例:

import numpy as np

import matplotlib

import matplotlib.pyplot as plt

# 这里是创建一个数据

vegetables = ["cucumber", "tomato", "lettuce", "asparagus",

"potato", "wheat", "barley"]

farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening",

"Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."]

harvest = np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],

[2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],

[1.1, 2.4, 0.8, 4.3, 1.9, 4.4, 0.0],

[0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],

[0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],

[1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],

[0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])

# 这里是创建一个画布

fig, ax = plt.subplots()

im = ax.imshow(harvest)

# 这里是修改标签

# We want to show all ticks...

ax.set_xticks(np.arange(len(farmers)))

ax.set_yticks(np.arange(len(vegetables)))

# ... and label them with the respective list entries

ax.set_xticklabels(farmers)

ax.set_yticklabels(vegetables)

# 因为x轴的标签太长了,需要旋转一下,更加好看

# Rotate the tick labels and set their alignment.

plt.setp(ax.get_xticklabels(), rotation=45, ha="right",

rotation_mode="anchor")

# 添加每个热力块的具体数值

# Loop over data dimensions and create text annotations.

for i in range(len(vegetables)):

for j in range(len(farmers)):

text = ax.text(j, i, harvest[i, j],

ha="center", va="center", color="w")

ax.set_title("Harvest of local farmers (in tons/year)")

fig.tight_layout()

plt.colorbar(im)

plt.show()

这类问题其实都不需要问的,多看看官网链接,就够了。Simple Colorbar - Matplotlib 3.3.3 documentation​pcolormesh - Matplotlib 3.3.3 documentation​Creating a colormap from a list of colors​Creating annotated heatmaps​

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。