使用数据框中的 matplotlib 并排绘制多个箱线图
multiple boxplots, side by side, using matplotlib from a dataframe
我正在尝试从一个数据框中并排绘制 60 多个箱线图,我想知道是否有人可以提出一些可能的解决方案。
目前我有 df_new
,一个包含 66 列的数据框,我用它来绘制箱线图。我发现绘制箱线图的最简单方法是使用 pandas:
中的箱线图包
boxplot = df_new.boxplot(column=x, figsize = (100,50))
这给了我一个非常非常小的图表,轴难以辨认,我似乎无法更改字体大小,所以我试图在 matplotlib 中本地执行此操作,但我想不出一种有效的方法。我试图避免使用类似以下内容创建 66 个单独的箱线图:
fig, ax = plt.subplots(nrows = 1,
ncols = 66,
figsize = (10,5),
sharex = True)
ax[0,0].boxplot(#insert parameters here)
我实际上不知道如何从 df_new.describe() 中获取数据到箱线图函数中,因此非常感谢任何关于此的提示! documentation 令人困惑。不确定 x 向量应该是什么。
理想情况下,我只想给箱线图函数提供数据框,让它通过动态计算所有四分位数、列分隔等自动创建所有箱线图 - 这甚至可能吗?
谢谢!
我尝试用 ridge plot
替换 boxplot
,它占用的空间更少 space 因为:
- 需要一半的宽度
- 你可以部分重叠脊线
- 它是垂直发展的,所以你可以向下滚动所有情节
我从 seaborn documentation 中获取代码并对其进行了一些调整,以便拥有 60 个不同的正态分布的脊;这里的代码:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import itertools
sns.set(style="white", rc={"axes.facecolor": (0, 0, 0, 0)})
# # Create the data
n = 20
x = list(np.random.randn(1, 60)[0])
g = [item[0] + item[1] for item in list(itertools.product(list('ABCDEFGHIJ'), list('123456')))]
df = pd.DataFrame({'x': n*x,
'g': n*g})
# Initialize the FacetGrid object
pal = sns.cubehelix_palette(10, rot=-.25, light=.7)
g = sns.FacetGrid(df, row="g", hue="g", aspect=15, height=.5, palette=pal)
# Draw the densities in a few steps
g.map(sns.kdeplot, "x", clip_on=False, shade=True, alpha=1, lw=1.5, bw=.2)
g.map(sns.kdeplot, "x", clip_on=False, color="w", lw=2, bw=.2)
g.map(plt.axhline, y=0, lw=2, clip_on=False)
# Define and use a simple function to label the plot in axes coordinates
def label(x, color, label):
ax = plt.gca()
ax.text(0, .2, label, fontweight="bold", color=color,
ha="left", va="center", transform=ax.transAxes)
g.map(label, "x")
# Set the subplots to overlap
g.fig.subplots_adjust(hspace=-.25)
# Remove axes details that don't play well with overlap
g.set_titles("")
g.set(yticks=[])
g.despine(bottom=True, left=True)
plt.show()
这是我得到的结果:
我不知道它是否适合您的需求,无论如何请记住,让这么多分布彼此相邻总是需要很多 space(而且非常大屏幕)。
也许您可以尝试将分布分成更小的组并一次绘制一点?
我正在尝试从一个数据框中并排绘制 60 多个箱线图,我想知道是否有人可以提出一些可能的解决方案。
目前我有 df_new
,一个包含 66 列的数据框,我用它来绘制箱线图。我发现绘制箱线图的最简单方法是使用 pandas:
boxplot = df_new.boxplot(column=x, figsize = (100,50))
这给了我一个非常非常小的图表,轴难以辨认,我似乎无法更改字体大小,所以我试图在 matplotlib 中本地执行此操作,但我想不出一种有效的方法。我试图避免使用类似以下内容创建 66 个单独的箱线图:
fig, ax = plt.subplots(nrows = 1,
ncols = 66,
figsize = (10,5),
sharex = True)
ax[0,0].boxplot(#insert parameters here)
我实际上不知道如何从 df_new.describe() 中获取数据到箱线图函数中,因此非常感谢任何关于此的提示! documentation 令人困惑。不确定 x 向量应该是什么。
理想情况下,我只想给箱线图函数提供数据框,让它通过动态计算所有四分位数、列分隔等自动创建所有箱线图 - 这甚至可能吗?
谢谢!
我尝试用 ridge plot
替换 boxplot
,它占用的空间更少 space 因为:
- 需要一半的宽度
- 你可以部分重叠脊线
- 它是垂直发展的,所以你可以向下滚动所有情节
我从 seaborn documentation 中获取代码并对其进行了一些调整,以便拥有 60 个不同的正态分布的脊;这里的代码:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import itertools
sns.set(style="white", rc={"axes.facecolor": (0, 0, 0, 0)})
# # Create the data
n = 20
x = list(np.random.randn(1, 60)[0])
g = [item[0] + item[1] for item in list(itertools.product(list('ABCDEFGHIJ'), list('123456')))]
df = pd.DataFrame({'x': n*x,
'g': n*g})
# Initialize the FacetGrid object
pal = sns.cubehelix_palette(10, rot=-.25, light=.7)
g = sns.FacetGrid(df, row="g", hue="g", aspect=15, height=.5, palette=pal)
# Draw the densities in a few steps
g.map(sns.kdeplot, "x", clip_on=False, shade=True, alpha=1, lw=1.5, bw=.2)
g.map(sns.kdeplot, "x", clip_on=False, color="w", lw=2, bw=.2)
g.map(plt.axhline, y=0, lw=2, clip_on=False)
# Define and use a simple function to label the plot in axes coordinates
def label(x, color, label):
ax = plt.gca()
ax.text(0, .2, label, fontweight="bold", color=color,
ha="left", va="center", transform=ax.transAxes)
g.map(label, "x")
# Set the subplots to overlap
g.fig.subplots_adjust(hspace=-.25)
# Remove axes details that don't play well with overlap
g.set_titles("")
g.set(yticks=[])
g.despine(bottom=True, left=True)
plt.show()
这是我得到的结果:
我不知道它是否适合您的需求,无论如何请记住,让这么多分布彼此相邻总是需要很多 space(而且非常大屏幕)。 也许您可以尝试将分布分成更小的组并一次绘制一点?