如何为多个数据集绘制小提琴图?
How to plot Violin plot for multiple dataset?
我有多个不同大小的数据集,我想根据它们绘制小提琴图。我的数据集如下所示:
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
注意:每个城市的集群规模和数量都不同。
我查看了一些帖子,例如 ,但我无法理解如何在一个图中绘制此数据集,例如:
seaborn 或 matplotlib 中的一些示例使用的是假数据,而我的数据是 CSV 格式,如上所示。如果您能提供像我这样使用数据的代码的帮助,那就太好了。
如果你有多个列表要绘制,你可以把它们作为列表的列表并绘制它们。您可以在此处阅读文档 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])
Seaborn 看起来像是一个更好、更美观的数据框解决方案。
from matplotlib import pyplot as plt
import seaborn as sns
fig, axes = plt.subplots(figsize=(5,5))
sns.set(style="whitegrid")
sns.violinplot(data=df, ax = axes, orient ='h')
我有多个不同大小的数据集,我想根据它们绘制小提琴图。我的数据集如下所示:
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
注意:每个城市的集群规模和数量都不同。
我查看了一些帖子,例如
seaborn 或 matplotlib 中的一些示例使用的是假数据,而我的数据是 CSV 格式,如上所示。如果您能提供像我这样使用数据的代码的帮助,那就太好了。
如果你有多个列表要绘制,你可以把它们作为列表的列表并绘制它们。您可以在此处阅读文档 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])
Seaborn 看起来像是一个更好、更美观的数据框解决方案。
from matplotlib import pyplot as plt
import seaborn as sns
fig, axes = plt.subplots(figsize=(5,5))
sns.set(style="whitegrid")
sns.violinplot(data=df, ax = axes, orient ='h')