gpt4 book ai didi

python - 使用 matplotlib 绘制向日葵散点图

转载 作者:太空狗 更新时间:2023-10-30 00:56:03 25 4
gpt4 key购买 nike

我对构建向日葵散点图很感兴趣(如 http://www.jstatsoft.org/v08/i03/paper [PDF 链接] 中所述)。在我编写自己的实现之前,有人知道现有的实现吗?我知道 Stata 和 R 中的函数,但正在寻找 matplotlib 中的函数。

谢谢。

最佳答案

我不知道有任何 matplotlib 实现,但这并不难做到。这里我让 hexbin 进行计数,然后遍历每个单元格并添加适当数量的花瓣:

enter image description here

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors

np.random.seed(0)
n = 2000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)

cmap = colors.ListedColormap(['white', 'yellow', 'orange'])
hb = plt.hexbin(x,y, bins='log', cmap=cmap, gridsize=20, edgecolor='gray')
plt.axis([-2, 2, -12, 12])
plt.title("sunflower plot")

counts = hb.get_array()
coords = hb.get_offsets()

for i, count in enumerate(counts):
x, y = coords[i,:]
count = int(10**count)
if count>3 and count<=12:
n = count // 1
if n>1:
plt.plot([x], [y], 'k.')
plt.plot([x], [y], marker=(n, 2), color='k', markersize=18)
if count>12:
n = count // 5
if n>1:
plt.plot([x], [y], 'k.')
plt.plot([x], [y], marker=(n, 2), color='k', markersize=18)

plt.show()

这里黄色是 1 个花瓣 = 1,橙色是 1 个花瓣 = 5。

这里一个明显需要改进的地方是使用颜色图。例如,您是要预设颜色边界还是从数据中计算它们等?在这里我只是稍微弄了一下:我使用 bins='log' 只是为了在我使用的特定样本中获得黄色和橙色细胞之间的合理比例;我还对白色、黄色和橙色单元格(3 和 12)之间的边界进行了硬编码。

能够使用元组来指定 matplotlib 中的标记特征使得绘制所有不同的花瓣数变得非常容易。

关于python - 使用 matplotlib 绘制向日葵散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22162557/

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