根据分布的宽敞,在matplotlib中更改小提琴图的颜色

发布于 2025-01-22 15:01:54 字数 589 浏览 3 评论 0原文

我一直在尝试使用Python中的Matplotlib库来绘制一个漂亮的小提琴图,以展示我的数据。这是我使用的代码:

ax.violinplot(vdistances,showmeans=False,showmedians=True)
ax.set_title('Distance analysis for molecule 1')
ax.set_xlabel('Atomic distances')
ax.set_ylabel('Distances in Amstrongs')
ax.set_xticks([x for x in range(1,len(distances)+1)])
plt.show()

这就是我想到的:

“在此处输入图像说明”

我一直想知道的是,是否可以为每个小提琴分配相同颜色的不同色调关于分布的宽敞,因此数据的分散程度越多。

I have been trying to work on plotting a nice violin plot to present my data using Matplotlib library in Python. This is the code I have used:

ax.violinplot(vdistances,showmeans=False,showmedians=True)
ax.set_title('Distance analysis for molecule 1')
ax.set_xlabel('Atomic distances')
ax.set_ylabel('Distances in Amstrongs')
ax.set_xticks([x for x in range(1,len(distances)+1)])
plt.show()

And this is what I have come up with:

enter image description here

What I have been wondering is if it is possible to assign different tones of the same color to each of the violins depending on how broad the distribution is, and so the more disperse the data is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈年往事 2025-01-29 15:01:54

您可以循环穿过生成的小提琴,提取其高度并使用它来设置颜色:

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
from matplotlib.cm import ScalarMappable
import numpy as np

fig, ax = plt.subplots(figsize=(12, 5))

vdistances = np.random.normal(np.random.uniform(10, 20, (20, 10)), np.random.uniform(2, 5, (20, 10)))
violins = ax.violinplot(vdistances, showmeans=False, showmedians=True)
ax.xaxis.set_major_locator(MultipleLocator(1))

heights = [violin.get_paths()[0].get_extents().height for violin in violins['bodies']]
norm = plt.Normalize(min(heights), max(heights))
cmap = plt.get_cmap('plasma')
for violin, height in zip(violins['bodies'], heights):
     violin.set_color(cmap(norm(height)))
     violin.set_alpha(0.5)

plt.colorbar(ScalarMappable(norm=norm, cmap=cmap), alpha=violin.get_alpha(), label='Violin Extent', ax=ax)
plt.tight_layout()
plt.show()

”带有颜色的小提琴图,具体取决于身高”

You could loop through the generated violins, extract their height and use that to set a color:

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
from matplotlib.cm import ScalarMappable
import numpy as np

fig, ax = plt.subplots(figsize=(12, 5))

vdistances = np.random.normal(np.random.uniform(10, 20, (20, 10)), np.random.uniform(2, 5, (20, 10)))
violins = ax.violinplot(vdistances, showmeans=False, showmedians=True)
ax.xaxis.set_major_locator(MultipleLocator(1))

heights = [violin.get_paths()[0].get_extents().height for violin in violins['bodies']]
norm = plt.Normalize(min(heights), max(heights))
cmap = plt.get_cmap('plasma')
for violin, height in zip(violins['bodies'], heights):
     violin.set_color(cmap(norm(height)))
     violin.set_alpha(0.5)

plt.colorbar(ScalarMappable(norm=norm, cmap=cmap), alpha=violin.get_alpha(), label='Violin Extent', ax=ax)
plt.tight_layout()
plt.show()

violin plots with color depending on height

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文