【问题标题】:Grouped boxplot with 2 y axes 2 variables per x tick带有 2 个 y 轴的分组箱线图 每个 x 刻度 2 个变量
【发布时间】:2021-01-03 01:22:44
【问题描述】:

我正在尝试制作成本(以卢比为单位)和装机容量(以兆瓦为单位)的箱线图,其中 xaxis 作为可再生能源的份额(以 % 为单位)。

也就是说,每个 x 刻度都与两个箱线图相关联,一个是成本,一个是装机容量。我有 3 个 xtick 值 (20%, 40%, 60%)

我尝试了this answer,但我得到了附加在底部的错误。

每个xtick 需要两个箱线图。

from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
plt.rcParams["font.family"] = "Times New Roman"
plt.style.use('seaborn-ticks')
plt.grid(color='w', linestyle='solid')
data1 = pd.read_csv('RES_cap.csv')
df=pd.DataFrame(data1, columns=['per','cap','cost'])

cost= df['cost']
cap=df['cap']
per_res=df['per']

fig, ax1 = plt.subplots()
xticklabels = 3

ax1.set_xlabel('Percentage of RES integration')
ax1.set_ylabel('Production Capacity (MW)')
res1 = ax1.boxplot(cost, widths=0.4,patch_artist=True)
for element in ['boxes', 'whiskers', 'fliers', 'means', 'medians', 'caps']:
    plt.setp(res1[element])

for patch in res1['boxes']:
    patch.set_facecolor('tab:blue')

ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis
ax2.set_ylabel('Costs', color='tab:orange')
res2 = ax2.boxplot(cap, widths=0.4,patch_artist=True)
for element in ['boxes', 'whiskers', 'fliers', 'means', 'medians', 'caps']:
    plt.setp(res2[element], color='k')
for patch in res2['boxes']:
    patch.set_facecolor('tab:orange')

ax1.set_xticklabels(['20%','40%','60%'])
fig.tight_layout()  
plt.show()

样本数据: data attached

【问题讨论】:

  • 您可以使用ax.set_ylim()ax.set_yticks() 来对齐两个轴的刻度
  • 错误是什么?我们没有您的数据,所以这可能是预期的图表。我们怎么知道?
  • 请不要将数据/代码/错误消息作为图片发布。您在互联网上向陌生人寻求帮助。您真的希望他们从图像中输入您的数据吗?除此之外 - 图像不包含数据类型。某些函数对 str(2)、float(2) 和 int(2) 的处理完全不同,但在图像中可能看起来相同。
  • 首先,欢迎来到 SO。正如the guidelines 中解释的那样,您应该通过creating an example 帮助我们重现您的问题,我们可以轻松使用,here 是有关如何执行此操作的一些提示。但是我理解您遇到的麻烦,因为我看到您已经直接跳入了 Python 绘图的深层。所以我在下面发布了一个带有假数据的答案,希望对您有所帮助。
  • 我很抱歉将问题与数据一起发布为图像,并且对于我遇到的错误也不是很准确。我应该通过stackoverflow.com/help/minimal-reproducible-example

标签: python matplotlib boxplot


【解决方案1】:

通过测试您的代码并将其与answer by Thomas Kühn in the linked question 进行比较,我发现了一些突出的地方:

  • 您为x 参数输入的数据具有一维形状而不是二维形状。您输入一个变量,因此您得到一个框,而不是您实际想要的三个;
  • positions 参数未定义,导致两个箱线图的框重叠;
  • res1 的第一个for 循环中,plt.setp 中的color 参数缺失;
  • 您设置了 x 个刻度标签而没有先设置 x 个刻度(如警告 here),这会导致错误消息。

我提供以下解决方案,它更多地基于this answer by ImportanceOfBeingErnest。它解决了正确塑造数据的问题,并利用字典来定义绘图中多个对象共享的许多参数。这使得根据您的喜好调整格式变得更容易,也使代码更简洁,因为它避免了对 for 循环(在箱线图元素和 res 对象上)的需要以及在共享相同的参数。

import numpy as np                 # v 1.19.2
import pandas as pd                # v 1.1.3
import matplotlib.pyplot as plt    # v 3.3.2

# Create a random dataset similar to the one in the image you shared
rng = np.random.default_rng(seed=123) # random number generator
data = dict(per = np.repeat([20, 40, 60], [60, 30, 10]),
            cap = rng.choice([70, 90, 220, 240, 320, 330, 340, 360, 410], size=100),
            cost = rng.integers(low=2050, high=2250, size=100))
df = pd.DataFrame(data)

# Pivot table according to the 'per' categories so that the cap and
# cost variables are grouped by them:
df_pivot = df.pivot(columns=['per'])

# Create a list of the cap and cost grouped variables to be plotted 
# in each (twinned) boxplot: note that the NaN values must be removed
# for the plotting function to work.
cap = [df_pivot['cap'][var].dropna() for var in df_pivot['cap']]
cost = [df_pivot['cost'][var].dropna() for var in df_pivot['cost']]

# Create figure and dictionary containing boxplot parameters that are
# common to both boxplots (according to my style preferences):
# note that I define the whis parameter so that values below the 5th
# percentile and above the 95th percentile are shown as outliers
nb_groups = df['per'].nunique()
fig, ax1 = plt.subplots(figsize=(9,6))
box_param = dict(whis=(5, 95), widths=0.2, patch_artist=True,
                 flierprops=dict(marker='.', markeredgecolor='black',
                 fillstyle=None), medianprops=dict(color='black'))

# Create boxplots for 'cap' variable: note the double asterisk used
# to unpack the dictionary of boxplot parameters
space = 0.15
ax1.boxplot(cap, positions=np.arange(nb_groups)-space,
            boxprops=dict(facecolor='tab:blue'), **box_param)

# Create boxplots for 'cost' variable on twin Axes
ax2 = ax1.twinx()
ax2.boxplot(cost, positions=np.arange(nb_groups)+space,
            boxprops=dict(facecolor='tab:orange'), **box_param)

# Format x ticks
labelsize = 12
ax1.set_xticks(np.arange(nb_groups))
ax1.set_xticklabels([f'{label}%' for label in df['per'].unique()])
ax1.tick_params(axis='x', labelsize=labelsize)

# Format y ticks
yticks_fmt = dict(axis='y', labelsize=labelsize)
ax1.tick_params(colors='tab:blue', **yticks_fmt)
ax2.tick_params(colors='tab:orange', **yticks_fmt)

# Format axes labels
label_fmt = dict(size=12, labelpad=15)
ax1.set_xlabel('Percentage of RES integration', **label_fmt)
ax1.set_ylabel('Production Capacity (MW)', color='tab:blue', **label_fmt)
ax2.set_ylabel('Costs (Rupees)', color='tab:orange', **label_fmt)

plt.show()

Matplotlib 文档:boxplot demoboxplot function parametersmarker symbols for flierslabel text formatting parameters

考虑到设置它需要付出很大的努力,如果我自己这样做,我会选择并排的子图而不是创建孪生轴。这可以在 seaborn 中使用 catplot 函数轻松完成,该函数会自动处理很多格式。由于每个变量只有三个类别,因此对于每个百分比类别使用不同的颜色并排比较箱线图相对容易,如基于相同数据的示例所示:

import seaborn as sns    # v 0.11.0

# Convert dataframe to long format with 'per' set aside as a grouping variable
df_melt = df.melt(id_vars='per')

# Create side-by-side boxplots of each variable: note that the boxes
# are colored by default
g = sns.catplot(kind='box', data=df_melt, x='per', y='value', col='variable',
                height=4, palette='Blues', sharey=False, saturation=1,
                width=0.3, fliersize=2, linewidth=1, whis=(5, 95))

g.fig.subplots_adjust(wspace=0.4)
g.set_titles(col_template='{col_name}', size=12, pad=20)

# Format Axes labels
label_fmt = dict(size=10, labelpad=10)
for ax in g.axes.flatten():
    ax.set_xlabel('Percentage of RES integration', **label_fmt)
g.axes.flatten()[0].set_ylabel('Production Capacity (MW)', **label_fmt)
g.axes.flatten()[1].set_ylabel('Costs (Rupees)', **label_fmt)

plt.show()

【讨论】:

  • 感谢您提供的两个解决方案。也因为如此彻底地解释了每一步。抱歉将数据发布为图像。尽管如此,还是感谢您解决问题。
猜你喜欢
  • 2023-03-10
  • 2020-07-10
  • 1970-01-01
  • 1970-01-01
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
  • 2012-04-26
  • 1970-01-01
相关资源
最近更新 更多