在小提琴图上绘制群图会更改 ylim 并截断小提琴

2023-12-29

import seaborn as sns
import numpy as np  # for sample data
import pandas as pd

# sample data
np.random.seed(365)
rows = 60
data1 = {'Type 1': ['a'] * rows,
         'Total': np.random.normal(loc=25, scale=3, size=rows)}
data2 = {'Type 1': ['b'] * rows,
         'Total': np.random.normal(loc=60, scale=7, size=rows)}
df = pd.concat([pd.DataFrame(d) for d in [data1, data2]], ignore_index=True)

# plot
plt.figure(figsize=(5, 4))
sns.violinplot(x='Type 1', y= 'Total', data=df, inner=None)
sns.swarmplot(x='Type 1', y= 'Total', data=df, color='#000000', size=3)

与没有的情节相比swarmplot

显示如上图,如何更改显示范围?

我试过了改变figsize https://i.stack.imgur.com/6D8ON.png. 我没有遇到这个问题,直到我重叠swarmplotvioletplot.

df

  Type 1      Total
0      a  25.503763
1      a  26.570516
2      a  27.452127
3      a  30.111537
4      a  18.559157
...
115      b  67.389032
116      b  67.337122
117      b  59.193256
118      b  56.356515
119      b  57.353019

  • When adding sns.swarmplot, or a sns.stripplot, to sns.violinplot, the limits of the y-axis are changed.
    • 使用这两种方法都会发生这种情况显式“轴”接口 https://matplotlib.org/stable/users/explain/api_interfaces.html#the-explicit-axes-interface, and 隐式“pyplot”接口 https://matplotlib.org/stable/users/explain/api_interfaces.html#the-implicit-pyplot-interface,如图所示plot https://i.stack.imgur.com/oJSwd.png.
    • Using sns.catplot with kind='violin', and .map_dataframe with sns.swarmplot也产生同样的问题,如此所示plot https://i.stack.imgur.com/maDK2.png.
    • 如果绘图,则不会发生这种情况sns.swarmplot on sns.boxplot,如图所示plot https://i.stack.imgur.com/J6c6g.png.
  • 测试于python 3.11.2, matplotlib 3.7.1, seaborn 0.12.2
import seaborn as sns
import matplotlib.pyplot as plt

# sample data
df = sns.load_dataset('geyser')

# plot
sns.violinplot(data=df, x='kind', y='duration', inner=None)
print('ylim with 1 plot', plt.ylim())
sns.swarmplot(data=df, x='kind', y='duration', color='#000000', size=3)
print('ylim with both plots', plt.ylim())
ylim with 1 plot (1.079871611291212, 5.607761736565478)
ylim with both plots (1.425, 5.2749999999999995)

解决

  • Here are three options to resolve the issue:
    1. 捕获ylim绘制后的值sns.violinplot,并设置ylim绘制后的这些值sns.swarmplot.
    2. Set ylim绘图后达到某个特定值sns.swarmplot
    3. Plot sns.swarmplot then sns.violinplot.
  • To have ylim从“原点”开始,使用y_bot = 0.
  • Using matplotlib.pyplot.ylim https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.ylim.html, matplotlib.axes.Axes.set_ylim https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.set_ylim.html, and matplotlib.axes.Axes.get_ylim https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.get_ylim.html.

1.

sns.violinplot(data=df, x='kind', y='duration', inner=None)
y_bot, y_top = plt.ylim()
sns.swarmplot(data=df, x='kind', y='duration', color='#000000', size=3)
plt.ylim(y_bot, y_top)

2.

sns.violinplot(data=df, x='kind', y='duration', inner=None)
sns.swarmplot(data=df, x='kind', y='duration', color='#000000', size=3)
plt.ylim(1, 6)

3.

# plot
sns.swarmplot(data=df, x='kind', y='duration', color='#000000', size=3)
print('ylim with 1 plot', plt.ylim())
sns.violinplot(data=df, x='kind', y='duration', inner=None)
print('ylim with both plots', plt.ylim())
ylim with 1 plot (1.425, 5.2749999999999995)
ylim with both plots (1.079871611291212, 5.607761736565478)

优先使用explicit界面

  • 为什么要明确? https://matplotlib.org/stable/users/explain/api_interfaces.html#why-be-explicit

plt.figure and .add_subplot

fig = plt.figure(figsize=(8, 5))
ax = fig.add_subplot()
sns.violinplot(data=df, x='kind', y='duration', inner=None, ax=ax)
y_bot, y_top = ax.get_ylim()
sns.swarmplot(data=df, x='kind', y='duration', color='#000000', size=3, ax=ax)
ax.set_ylim(y_bot, y_top)

plt.subplots

fig, axes = plt.subplots(figsize=(8, 5))
sns.violinplot(data=df, x='kind', y='duration', inner=None, ax=ax)
y_bot, y_top = ax.get_ylim()
sns.swarmplot(data=df, x='kind', y='duration', color='#000000', size=3, ax=ax)
ax.set_ylim(y_bot, y_top)

df[['duration', 'kind']].head()

  • 这个真实数据与OP中的随机样本类似。
   duration   kind
0     3.600   long
1     1.800  short
2     3.333   long
3     2.283  short
4     4.533   long
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在小提琴图上绘制群图会更改 ylim 并截断小提琴 的相关文章

随机推荐

  • 如何在 Mac Os X 上将用户添加到 apache 组?

    我实际上正在尝试在我的 MacBook Pro Mac OS X 10 6 上运行 Symfony2 当我通过 apache 启动应用程序时 所有缓存和日志文件都是由 www www 用户 组创建的 但是 我已经必须使用我自己的用户在 CL
  • 使用 rvest 跟随带有相对路径的“下一个”链接

    我正在使用rvest从页面中抓取信息的包http www radiolab org series podcasts http www radiolab org series podcasts 抓取第一页后 我想点击底部的 下一步 链接 抓取
  • 可以将PreparedStatement.addBatch()用于SELECT查询吗?

    想象一下 我有 100 个 SELECT 查询 它们因一个输入而异 可以使用PreparedStatement 作为该值 我在网上看到的所有文档都是关于批量插入 更新 删除的 我从未见过用于 select 语句的批处理 这可以做到吗 如果是
  • 有哪些好的实体框架替代品[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我现在正在使用实体框架 并且经常需要编写内联sql 因为实体框架不支持全文搜索和其他功能 是否有一个 ORM 具有许多支持高级查询的功能
  • Wix 和 .NET Framework(先决条件)

    当客户端计算机上尚未安装所需的 NET Framework 时 如何让 Wix 包下载该包 我已经有条件检查已安装的 NET 版本 但我不确定如何在找不到时下载并安装它 ClickOnce 通过检查属性页中的先决条件自动执行此操作 由于一些
  • android-viewflipper 的简单淡出和淡入动画

    我是android新手 对android动画了解不多 我有一个取景器 我想在其中的图像之间制作动画 这是代码 runnable new Runnable public void run handler postDelayed runnabl
  • Oracle,向字符串(不是数字)添加前导零

    我正在使用 Oracle 工作空间是 TOAD 我需要将字符串设置为短接 10 个字符 然后添加前导零以使它们全部为 10 位数字字符串 例如 如果我有一个像这样的字符串 12H89 需要是 0000012H89 或者 1234 变为 00
  • C++中**是什么意思? [复制]

    这个问题在这里已经有答案了 例如 bool insertInFront IntElement head int data IntElement newElem new IntElement if newElem return false n
  • Google 通讯录 api (gdata) 同步低分辨率照片

    我正在使用 google 联系人 api gdata 在 google 联系人中设置联系人的照片 我正在使用 fiddler 我看到请求是根据Google 通讯录示例 https developers google com google a
  • Angular Spectator setInput 不适用于非字符串输入

    我已经成功地将我的项目转换为使用 Jest 代替 Karma Jasmine 并且我有很多测试运行得很好 我正在尝试使用 Spectator 5 2 1 进行一个非常简单的测试 但它不起作用 我正在尝试测试使用 mat table 呈现表格
  • Rails 路由的 API 版本控制

    我正在尝试像 Stripe 那样对我的 API 进行版本控制 下面给出的最新 API 版本是 2 api users返回 301 api v2 users api v1 users返回版本 1 的 200 个用户索引 api v3 user
  • 多条件IF语句

    我有一个包含多个条件的 if 语句 但我似乎无法正确执行 if ISSET SESSION status SESSION username qqqqq ISSET SESSION status SESSION company wwwwww
  • Kotlin 中通过反射获取 Enum 值

    您将如何用 Kotlin 重写以下 Java 代码 SuppressWarnings unchecked rawtypes static Object getEnumValue String enumClassName String enu
  • 如何将顶视图折叠成较小尺寸的视图?

    这个问题之前曾以过于宽泛和不清楚的方式提出过here https stackoverflow com q 47053822 878126 所以我使它更加具体 并提供了我所尝试的完整解释和代码 背景 我需要模仿谷歌日历在顶部有一个视图的方式
  • JavaScript 中的构造函数或对象继承

    我是 JavaScript 新手 本周开始学习 我已经完成了 CodeCademy 课程 实际上只是对象 1 2 部分 其余的很无聊 我以为我学会了构造函数的原型继承 但我已经开始观看了Douglas Crockford 高级 JavaSc
  • 在两个curl请求之间保存cookie

    我知道使用cURL我可以使用以下命令查看收到的 cookie 标头 curl head www google com 我知道我可以使用以下方法将标头添加到我的请求中 curl cookie Key Value www google com
  • Android 以编程方式重置出厂设置

    我尝试使用 RecoverySystem 类在 Android 中执行恢复出厂设置 但出现权限错误 但我无法覆盖这些错误 因为它们是系统权限 我想知道是否还有其他方法可以恢复出厂设置 第三方应用程序绝对可以做到这一点 在 2 2 设备 包括
  • 将 LUIS 对话框连接到表单对话框并映射正确的字段

    我正在开发一个可以预订航班的机器人 我正在使用最新版本的机器人框架 1 1 如建议 https stackoverflow com questions 36712912 mapping luis entities to dialog fie
  • 特征返回特征:在某些情况下有效,在其他情况下无效

    我需要实现一个返回的特征futures StreamExt trait 一般来说 这听起来很简单 并且有几个答案 例如这里 https stackoverflow com questions 60143046 how can a rust
  • 在小提琴图上绘制群图会更改 ylim 并截断小提琴

    import seaborn as sns import numpy as np for sample data import pandas as pd sample data np random seed 365 rows 60 data