gpt4 book ai didi

python - 如何绘制这个图?

转载 作者:太空宇宙 更新时间:2023-11-03 15:53:00 25 4
gpt4 key购买 nike

enter image description here

我想使用 python 绘制类似上面的图。我喜欢一个功能:

该图形被分成几个不同颜色(和数字)的矩形。我能想到的唯一近似图是散点图。但散点图显示了一些点,而不是矩形。

谁能帮我吗?

最佳答案

答案可能取决于您想要显示哪种数据。有多种方法可以生成这样的图,

  1. 使用 Rectangle具有给定顶点的 s
  2. 使用 imshow等距网格上的数组
  3. 使用 pcolormesh不等距网格上的数组

假设您想要绘制直方图并选择第三个选项,可能的解决方案可能如下所示(基于 histogram2d )

import matplotlib.pyplot as plt
import numpy as np

xedges = [0, 1, 1.5, 3, 5]
yedges = [0, 2, 3, 4, 6]

# produce histogram
x = np.random.normal(2.5, 1, 100)
y = np.random.normal(1.5, 1, 100)
H, xedges, yedges = np.histogram2d(y, x, bins=(xedges, yedges))

fig=plt.figure()
ax = fig.add_subplot(111)
ax.set_title('Something')
X, Y = np.meshgrid(xedges, yedges)
im = ax.pcolormesh(X, Y, H)

# label the histogram bins
for i in range(len(yedges)-1):
for j in range(len(xedges)-1):
ax.text( (xedges[j+1]-xedges[j])/2.+xedges[j] ,
(yedges[i+1]-yedges[i])/2.+yedges[i] ,
str(H[i, j]) , ha="center", va="center", color="w", fontweight="bold")
plt.colorbar(im)

plt.show()

enter image description here

关于python - 如何绘制这个图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41087705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com