Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带2个y轴的分组箱线图,每个x记号有2个打印变量_Python_Matplotlib_Seaborn_Boxplot - Fatal编程技术网

Python 带2个y轴的分组箱线图,每个x记号有2个打印变量

Python 带2个y轴的分组箱线图,每个x记号有2个打印变量,python,matplotlib,seaborn,boxplot,Python,Matplotlib,Seaborn,Boxplot,我正试图做一个18年来每月降雨量和洪水频率记录的箱线图。i、 e.每个x记号表示月份,每个x记号与两个箱线图相关,一个是降雨量,一个是洪水频率。到目前为止,我已经使用seaborn(参见下面的代码和图像)绘制了这些曲线,但是我不知道如何创建带有两个y轴的箱线图,因为每个变量的比例不同,所以我需要使用这两个y轴 数据如下所示(数据集中flood_freq的最大值为7,此处未显示): 这是我使用的代码: dd=pd.melt(FBPdf,id_vars=['Group'],value_vars=['

我正试图做一个18年来每月降雨量和洪水频率记录的箱线图。i、 e.每个x记号表示月份,每个x记号与两个箱线图相关,一个是降雨量,一个是洪水频率。到目前为止,我已经使用seaborn(参见下面的代码和图像)绘制了这些曲线,但是我不知道如何创建带有两个y轴的箱线图,因为每个变量的比例不同,所以我需要使用这两个y轴

数据如下所示(数据集中flood_freq的最大值为7,此处未显示):

这是我使用的代码:

dd=pd.melt(FBPdf,id_vars=['Group'],value_vars=['Rainfall','Flood_freq'],var_name='Data')
sns.boxplot(x='Group',y='value',data=dd,hue='Data')
其结果是:

我查看了seaborn文档,它似乎不允许使用2个y轴()。有没有人能为我想要实现的目标提供潜在的替代方案?上面链接上的解决方案与我遇到的双y轴和分组箱线图问题无关


提前非常感谢

通过一些虚假数据和来自和的一些帮助,这里有一个简单的示例,说明如何仅使用
numpy
matplotlib
实现所需:

from matplotlib import pyplot as plt
import numpy as np

rainfall = np.random.rand((12*18))*300
floods =   np.random.rand((12*18))*2

t = np.arange(0.01, 10.0, 0.01)
data1 = np.exp(t)
data2 = np.sin(2 * np.pi * t)

fig, ax1 = plt.subplots()

months = [
    'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
]


ax1.set_xlabel('month')
ax1.set_ylabel('rainfall', color='tab:blue')
res1 = ax1.boxplot(
    rainfall.reshape(-1,12), positions = np.arange(12)-0.25, widths=0.4,
    patch_artist=True,
)
for element in ['boxes', 'whiskers', 'fliers', 'means', 'medians', 'caps']:
    plt.setp(res1[element], color='k')

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('floods', color='tab:orange')
res2 = ax2.boxplot(
    floods.reshape(-1,12), positions = np.arange(12)+0.25, widths=0.4,
    patch_artist=True,
)
##from https://stackoverflow.com/a/41997865/2454357
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_xlim([-0.55, 11.55])
ax1.set_xticks(np.arange(12))
ax1.set_xticklabels(months)

fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.show()
结果如下所示:


我认为只要稍微微调一下,这实际上看起来很不错。

通过一些虚假数据和来自的一些帮助,这里有一个简单的示例,说明如何仅使用
numpy
matplotlib
实现您想要的功能:

from matplotlib import pyplot as plt
import numpy as np

rainfall = np.random.rand((12*18))*300
floods =   np.random.rand((12*18))*2

t = np.arange(0.01, 10.0, 0.01)
data1 = np.exp(t)
data2 = np.sin(2 * np.pi * t)

fig, ax1 = plt.subplots()

months = [
    'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
]


ax1.set_xlabel('month')
ax1.set_ylabel('rainfall', color='tab:blue')
res1 = ax1.boxplot(
    rainfall.reshape(-1,12), positions = np.arange(12)-0.25, widths=0.4,
    patch_artist=True,
)
for element in ['boxes', 'whiskers', 'fliers', 'means', 'medians', 'caps']:
    plt.setp(res1[element], color='k')

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('floods', color='tab:orange')
res2 = ax2.boxplot(
    floods.reshape(-1,12), positions = np.arange(12)+0.25, widths=0.4,
    patch_artist=True,
)
##from https://stackoverflow.com/a/41997865/2454357
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_xlim([-0.55, 11.55])
ax1.set_xticks(np.arange(12))
ax1.set_xticklabels(months)

fig.tight_layout()  # otherwise the right y-label is slightly clipped
plt.show()
结果如下所示:


我认为,只要稍加微调,这实际上可以看起来相当不错。

您有什么特别的原因想使用seaborn来实现这一点吗?仅使用matplotlib和numpy是完全可行的。您有什么特别的原因要使用seaborn来实现这一点吗?仅使用matplotlib和numpy就完全可以做到这一点。