seaborn - 如何在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)
太感谢了!
解决方案
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()
推荐阅读
- json - 使用 JSONPATHS 文件到 Redshift 的 JSON 文件 - 无效的 jsonpath 格式
- python-3.x - 在 ubuntu 服务器中安装 deepspeech 时出错
- arrays - 创建一个javascript数组到localstorage不返回所需的结果
- database-design - 具有可编辑部分的复合主键
- django - 如何在 django rest 框架嵌套序列化程序中验证需要来自父级数据的子序列化程序?
- python - 如何将多行缩进脚本作为字符串传递给python解释器(-c)
- python - 带有 fit_generator 的 Keras 错误:检查目标时出错:预期 softmax_1 的形状为 (2,),但得到的数组的形状为 (1,)
- c++ - C++ 中指针使用的箭头标记“->”有不同的用途吗?它究竟是如何使用的?
- node.js - 节点包管理器测试用例问题
- php - laravel 查询 - 在两个相关表之间使用多个条件约束