【发布时间】:2019-08-05 19:42:32
【问题描述】:
我正在使用一个名为 ptitprince 的 github 存储库,它源自 seaborn 和 matplotlib,用于生成图形。
例如,这是使用 ptitprince 存储库的代码:
# coding: utf8
import pandas as pd
import ptitprince as pt
import seaborn as sns
import os
import matplotlib.pyplot as plt
#sns.set(style="darkgrid")
#sns.set(style="whitegrid")
#sns.set_style("white")
sns.set(style="whitegrid",font_scale=2)
import matplotlib.collections as clt
df = pd.read_csv ("u118phag.csv", sep= ",")
df.head()
savefigs = True
figs_dir = 'figs'
if savefigs:
# Make the figures folder if it doesn't yet exist
if not os.path.isdir('figs'):
os.makedirs('figs')
#automation
f, ax = plt.subplots(figsize=(4, 5))
#f.subplots_adjust(hspace=0,wspace=0)
dx = "Treatment"; dy = "score"; ort = "v"; pal = "Set2"; sigma = .2
ax=pt.RainCloud(x = dx, y = dy, data = df, palette = pal, bw = sigma,
width_viol = .6, ax = ax, move=.2, offset=.1, orient = ort, pointplot = True)
f.show()
if savefigs:
f.savefig('figs/figure20.png', bbox_inches='tight', dpi=500)
which generates the following graph
未使用 ptitprince 的原始代码如下,并生成与上面相同的图形:
# coding: utf8
import pandas as pd
import ptitprince as pt
import seaborn as sns
import os
import matplotlib.pyplot as plt
#sns.set(style="darkgrid")
#sns.set(style="whitegrid")
#sns.set_style("white")
sns.set(style="whitegrid",font_scale=2)
import matplotlib.collections as clt
df = pd.read_csv ("u118phag.csv", sep= ",")
df.head()
savefigs = True
figs_dir = 'figs'
if savefigs:
# Make the figures folder if it doesn't yet exist
if not os.path.isdir('figs'):
os.makedirs('figs')
f, ax = plt.subplots(figsize=(7, 5))
dy="Treatment"; dx="score"; ort="h"; pal = sns.color_palette(n_colors=1)
#adding color
pal = "Set2"
f, ax = plt.subplots(figsize=(7, 5))
ax=pt.half_violinplot( x = dx, y = dy, data = df, palette = pal, bw = .2, cut = 0.,
scale = "area", width = .6, inner = None, orient = ort)
ax=sns.stripplot( x = dx, y = dy, data = df, palette = pal, edgecolor = "white",
size = 3, jitter = 1, zorder = 0, orient = ort)
ax=sns.boxplot( x = dx, y = dy, data = df, color = "black", width = .15, zorder = 10,\
showcaps = True, boxprops = {'facecolor':'none', "zorder":10},\
showfliers=True, whiskerprops = {'linewidth':2, "zorder":10},\
saturation = 1, orient = ort)
if savefigs:
f.savefig('figs/figure21.png', bbox_inches='tight', dpi=500)
现在,我要做的是弄清楚如何修改图表,以便我可以 (1) 将绘图移得更近,因此它们之间没有太多空白,以及 (2) 移位x 轴向右,这样我就可以使分布(小提琴)图更宽,而不会被 y 轴切成两半。
我尝试过使用subplots_adjust(),正如您在第一个代码框中看到的那样,但我收到了一个错误。我不知道如何正确使用此功能,或者即使这实际上会使不同的图表更接近。
我也知道我可以通过增加这个值width = .6 来增加分布大小,但是如果我把它增加得太高,分布图就会开始被 y 轴截断。我不知道是否需要使用plt.subplots 调整整体图,或者是否需要移动每个单独的图。
关于如何更改图表的视觉效果有什么建议或建议吗?我已经盯着这个看了一段时间,我不知道如何让 seaborn/matplotlib 与 ptitprince 很好地配合。
【问题讨论】:
标签: python matplotlib graph seaborn graph-visualization