首页 > 解决方案 > 如何在seaborn中仅绘制误差条的正面?

问题描述

我正在尝试绘制图表,但我有一些问题要解决,对不起,但我是程序语言的新手。第一个:如何只绘制一个图表?我从互联网上得到了那个例子,当一个图有两个数字时,每个代码都有两个数字,其中两个是空白的。

第二个:是否可以只绘制正误差条?
第三个:这两个图表可以并排绘制在一个图中吗?

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt


Treat1 =pd.DataFrame({'Treatment': 1, 'weight': np.random.randint(low=1, high=100, size=40)})              
Treat2 =pd.DataFrame({'Treatment': 2, 'weight': np.random.randint(low=1, high=100, size=40)}) 

df = pd.concat([Treat1, Treat2])
Treat3 =pd.DataFrame({'Treatment': 1, 'weight': np.random.randint(low=100, high=300, size=40)})              
Treat4 =pd.DataFrame({'Treatment': 2, 'weight': np.random.randint(low=100, high=300, size=40)}) 

df2 = pd.concat([Treat3, Treat4])

sns.set(style="ticks")
fig, ax = plt.subplots()

color_map = dict(pos="indianred", neg="steelblue")
g = sns.catplot(x= "Treatment", y="weight",  hue="Treatment", capsize=.07, ci ="sd", 
                data=df,  kind="bar", palette = 'coolwarm', edgecolor="white")
plt.text(-0.22,99, "B")
plt.text(1.18,99, "A")
plt.ylabel('weight, kg')
plt.xticks([-0.2, 1.2], ['Group 1', 'Group 2'])
plt.ylim(0, 100)

color_map = dict(pos="indianred", neg="steelblue")
g = sns.catplot(x= "Treatment", y="weight",  hue="Treatment", capsize=.07, ci ="sd", 
                data=df2,  kind="bar", palette = 'coolwarm', edgecolor="white")
plt.text(-0.22,300, "B")
plt.text(1.18,300, "A")
plt.ylabel('weight, kg')
plt.xticks([-0.2, 1.2], ['Group 1', 'Group 2'])
plt.ylim(0, 300)

太感谢了!

标签: seaborn

解决方案


seaborncatplot是一个图形级别的情节,它创建并占据了一个新图形。要有这样的情节作为子情节,sns.barplot可以直接调用。提供一个ax告诉条形图应该进入哪个子图。

得到一个图例,在这种barplot情况下是多余的,但可以将其删除。

要仅使上部误差条可见,可以将条形的矩形绘制在它们的顶部。zorder大于zorder误差条 (2) 的线会处理此问题。

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

Treat1 = pd.DataFrame({'Treatment': 1, 'weight': np.random.randint(low=1, high=100, size=40)})
Treat2 = pd.DataFrame({'Treatment': 2, 'weight': np.random.randint(low=1, high=100, size=40)})
df1 = pd.concat([Treat1, Treat2])

Treat3 = pd.DataFrame({'Treatment': 1, 'weight': np.random.randint(low=100, high=300, size=40)})
Treat4 = pd.DataFrame({'Treatment': 2, 'weight': np.random.randint(low=100, high=300, size=40)})
df2 = pd.concat([Treat3, Treat4])

sns.set(style="ticks")
fig, axs = plt.subplots(ncols=2, figsize=(10, 4))

for ax, df, height in zip(axs, [df1, df2], [100, 300]):

    color_map = {1: "indianred", 2: "steelblue"}
    g = sns.barplot(x="Treatment", y="weight", hue="Treatment", capsize=.07, ci="sd",
                    data=df, palette=color_map, edgecolor="white", ax=ax)
    g.legend_.remove()
    for bar in g.patches:
        bar.set_zorder(3)
    ax.text(-0.2, height * 0.95, "B", ha='center')
    ax.text(1.2, height * 0.95, "A", ha='center')
    ax.set_ylabel('weight, kg')
    ax.set_xticks([-0.2, 1.2])
    ax.set_xticklabels(['Group 1', 'Group 2'])
    ax.set_ylim(0, height)

plt.tight_layout()
plt.show()

示例图

PS:请注意,如果您不使用hue=. 这也使条形图处于更合乎逻辑的位置。


fig, axs = plt.subplots(ncols=2, figsize=(10, 4))

for ax, df, height in zip(axs, [df1, df2], [100, 300]):

    color_map = {1: "indianred", 2: "steelblue"}
    g = sns.barplot(x="Treatment", y="weight", capsize=.07, ci="sd",
                    data=df, palette=color_map, edgecolor="white", ax=ax)
    for bar in g.patches:
        bar.set_zorder(3)
    ax.text(0, height * 0.97, "B", ha='center', va='top')
    ax.text(1, height * 0.97, "A", ha='center', va='top')
    ax.set_ylabel('weight, kg')
    ax.set_ylim(0, height)
    ax.set_xticklabels(['Group 1', 'Group 2'])

plt.tight_layout()
plt.show()

第二个情节


推荐阅读