【发布时间】:2021-07-22 14:06:29
【问题描述】:
背景
我正在使用sns.regplot(seaborn 0.11.0,Python 3.8.5)绘制我的数据。我使用参数“x_estimator”来绘制 x 轴上显示的每个类别的平均值,对于 x 轴上的每个点,我都有一个错误栏,它使用sns.regplot 参数“ci”和“boot”引导.
由于此图需要具有 800 的特定每英寸点数 (DPI),因此我需要重新调整原始图的缩放比例以确保获得所需的 DPI。
问题
由于重新缩放,我的错误栏似乎相当“宽”。我想让它们不那么宽,如果可能的话,我还想在错误栏上添加大写。我使用随机生成的数据集在下面包含了我的代码。运行此代码,可以看到plot that I obtain 具有正确的 DPI,但是错误栏太宽了。
编辑澄清
我对置信区间 (CI) 本身很好。我唯一担心的是 CI 有点太宽了。这可能是一些格式问题。我已经检查了line_kws 和scatter_kws,但我找不到 CI 的任何格式选项。我想要的输出看起来像this:相同的条,但不像原来的那样“重”。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#%%
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib import rcParams
#%%
# seaborn params
sns.set_style("ticks")
sns.set_context("paper")
# plotting params
rcParams['font.family'] = 'Times New Roman'
rcParams['axes.titlesize'] = 6
rcParams['axes.labelsize'] = 5
rcParams['xtick.labelsize'] = 5
rcParams['ytick.labelsize'] = 5
#%%
# some toy data into to pandas dataframe
df = pd.DataFrame({'Y': np.random.normal(0, 1, (800,)),
'X': np.repeat(range(1, 9), 100),
'Condition': np.tile(["A", "B"], 400)},
index=range(800))
#%%
# make a subplot with 1 row and 2 columns
fig, ax_list = plt.subplots(1, 2,
sharex = True,
sharey = True,
squeeze = True)
# A condition
g = sns.regplot(x = "X",
y = "Y",
data = df.loc[df["Condition"] == "A"],
x_estimator = np.mean,
x_ci = "ci",
ci = 95,
n_boot = 5000,
scatter_kws = {"s":15},
line_kws = {'lw': .75},
color = "darkgrey",
ax = ax_list[0])
# B condition
g = sns.regplot(x = "X",
y = "Y",
data = df.loc[df["Condition"] == "B"],
x_estimator = np.mean,
x_ci = "ci",
ci = 95,
n_boot = 5000,
scatter_kws = {"s":15},
line_kws = {'lw': .75},
color = "black",
ax = ax_list[1])
# figure parameters (left figure)
ax_list[0].set_title("A condition")
ax_list[0].set_xticks(np.arange(1, 9))
ax_list[0].set_xlim(0.5, 8.5)
ax_list[0].set_xlabel("X")
ax_list[0].set_ylabel("Y")
# figure parameters (right figure)
ax_list[1].set_title("B condition")
ax_list[1].set_xlabel("X")
ax_list[1].set_ylabel("Y")
# general title
fig.suptitle("Y ~ X", fontsize = 8)
#%%
# set the size of the image
fig.set_size_inches(3, 2)
# play around until the figure is satisfactory (difficult due to high DPI)
plt.subplots_adjust(top=0.85, bottom=0.15, left=0.185, right=0.95, hspace=0.075,
wspace=0.2)
# save as tiff with defined DPI
plt.savefig(fname = "test.tiff", dpi = 800)
plt.close("all")
【问题讨论】:
-
一个思路是把图形的宽和高乘以4,使用dpi=200,然后使用图像处理程序将dpi修改为800。
-
谢谢。那么有没有办法只改变 CI 的格式?我可以使用其他程序,但如果一切都可以在没有其他软件的情况下用 Python 完成,我会更喜欢。
标签: python python-3.x seaborn