如何绘制多个数据集的小提琴图?

2024-09-27 00:15:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有多个不同大小的数据集,我想从中绘制小提琴图。我的数据集如下所示:

Input.CSV:

         city_A city_B  city C  city_D
cluster1   2       5      4      4
cluster2   3       3      2      8
cluster3   2       4      5      5
cluster4   3       5      4
cluster5           3      3
cluster6           5

注:每个城市都有不同的规模和数量的集群。在

我查阅了一些文章,如here,我不明白如何在一个图中绘制此数据集,如:

enter image description here

seaborn或matplotlib中的一些示例包含伪数据,而我的数据是CSV格式的,如我上面所示。如果你能在使用像我这样的数据的代码方面提供帮助,那就太好了。在


Tags: csv数据cityinput数量文章绘制集群
1条回答
网友
1楼 · 发布于 2024-09-27 00:15:03

如果要绘制多个列表,可以将它们作为列表列表并打印。您可以在这里阅读文档https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.violinplot.html

from matplotlib import pyplot as plt

A = [2, 5, 6, 10, 12, 8, 5]
B = [2, 2, 6,  8, 14, 5, 5]
C = [5, 7, 5, 13, 17, 7, 5]
D = [1, 4, 7, 12, 12, 5, 5]
E = [4, 1, 2, 11, 13, 7, 5]

fig, ax = plt.subplots(figsize=(5,5))
ax.violinplot([A,B,C,D,E][::-1],positions =[5,4,3,2,1],vert=False,showmeans=True)

def set_axis_style(ax, labels):
    ax.get_yaxis().set_tick_params(direction='out')
    ax.xaxis.set_ticks_position('bottom')
    ax.set_yticks(np.arange(1, len(labels) + 1))
    ax.set_yticklabels(labels)
    ax.set_ylim(0.25, len(labels) + 0.75)
    ax.set_ylabel('Sample name')

set_axis_style(ax,['A','B','C','D','E'][::-1])

enter image description here

Seaborn看起来是一个更好、更美观的数据帧解决方案。在

^{pr2}$

enter image description here

相关问题 更多 >

    热门问题