python - 第二个 y 轴和垂直线
问题描述
我正在使用以下代码创建小提琴图:
import seaborn as sns
ax = sns.violinplot(data=df[['SoundProduction','SoundForecast','diff']])
ax.set_ylabel("Sound power level [dB(A)]")
它给了我以下结果:
有什么办法可以diff
在第二个 y 轴上绘制,以便所有三个系列都清晰可见?
另外,有没有办法在两个系列之间绘制一条垂直线?在这种情况下,我想要一条垂直线SoundForecast
,diff
一旦它们被绘制在两个不同的轴上。
解决方案
您可以使用多个子图来实现这一点,这些子图可以使用plt.subplots轻松设置(请参阅更多子图示例)。
这允许您以适当的比例显示您的分布,并且不会“浪费”显示空间。大多数(全部?)seaborn 的绘图函数都接受该ax=
参数,因此您可以设置要渲染绘图的轴。轴之间也有明显的分隔。
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# generate some random distribution data
n = 800 # samples
prod = 95 + 5 * np.random.beta(0.6, 0.5, size=n); # a bimodal distribution
forecast = prod + 3*np.random.randn(n) # forecast is noisy estimate around the "true" production
diff = prod-forecast # should be with mu 0 sigma 3
df = pd.DataFrame(np.array([prod, forecast, diff]).T, columns=['SoundProduction','SoundForecast','diff']);
# set up two subplots, with one wider than the other
fig, ax = plt.subplots(1,2, num=1, gridspec_kw={'width_ratios':[2,1]})
# plot violin distribution estimates separately so the y-scaling makes sense in each group
sns.violinplot(data=df[['SoundProduction','SoundForecast']], ax=ax[0])
sns.violinplot(data=df[['diff']], ax=ax[1])
推荐阅读
- python - 如何将分割结果叠加在其他图像上
- javascript - 如何使用 javascript 或 jquery 访问手电筒(手电筒)?
- php - 程序php中的mysql查询不返回单个字段
- google-cloud-platform - Hbase Table 迁移到 Google Bigtable
- reactjs - 如何使用 Stripe 和 NextJS(或 React)构建持久的 Checkout 页面?
- react-redux - 如何使用 createAEntityAdapter 设置 initialState
- mysql - 从副本恢复数据库
- azure-devops - Azure Synapse SQL DW 部署是否类似于 SQL DB 部署?
- arrays - 如何在 TypeOrm 实体中添加枚举数组?
- html - 播放视频后如何更改视频栏的背景颜色