AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 78551186
Accepted
ohshitgorillas
ohshitgorillas
Asked: 2024-05-30 01:42:26 +0800 CST2024-05-30 01:42:26 +0800 CST 2024-05-30 01:42:26 +0800 CST

Matplotlib布局问题

  • 772

我正在用 Python v3.7.9 编写一些质谱数据缩减软件,并使用 matplotlib v3.5.3 来显示数据。

我最近发现我使用了错误的模块,matplotlib.pyplot它与 tkinter 不兼容。我现在尝试切换到 using matplotlib.figure,这是兼容的(参考)。

这导致了几个问题:

  • 现在布局更加紧凑,导致轴几乎重叠,并且绘图周围有大量空白。
  • 我无法添加副标题 ( AttributeError: module 'matplotlib.figure' has no attribute 'suptitle'),即使matplotlib 文档中明确定义了该属性。

以下是一些前后的屏幕截图:第一个使用matplotlib.pyplot紧凑布局,第二个使用matplotlib.figure默认布局选项。

matplotlib.pyplot matplotlib.figure

手动将布局设置为“紧密”或“约束”似乎根本没有太大效果,也不定义布局样式。

有没有一种方法可以恢复以前的“扩展”布局样式,而无需手动定义大量参数?

这是一个可以使用的示例脚本:

import customtkinter as ctk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

import matplotlib.figure as Figure

# create the window using CTK
window = ctk.CTk()

# create the plot of random data
figure = Figure.Figure(figsize=(15,9))
Figure.layout = 'tight'

# create the subplots
ax1 = figure.add_subplot(2, 3, 1)
ax2 = figure.add_subplot(2, 3, 2)
ax3 = figure.add_subplot(2, 3, 3)
ax4 = figure.add_subplot(2, 3, 4)
ax5 = figure.add_subplot(2, 3, 5)
ax6 = figure.add_subplot(2, 3, 6)

# plot data in each subplot
data = [1, 2, 3, 4, 5]
ax1.plot(data)
ax2.plot(data)
ax3.plot(data)
ax4.plot(data)
ax5.plot(data)
ax6.plot(data)

# add titles to each subplot
ax1.set_title('Subplot 1')
ax2.set_title('Subplot 2')
ax3.set_title('Subplot 3')
ax4.set_title('Subplot 4')
ax5.set_title('Subplot 5')
ax6.set_title('Subplot 6')

# Add the plot to the window
canvas = FigureCanvasTkAgg(figure, master=window)
canvas.draw()
canvas.get_tk_widget().pack(side='top', fill='both', expand=True)

exit_button = ctk.CTkButton(window, text='Exit', command=window.destroy)
exit_button.pack(side='bottom')  # add the exit button to the window

window.mainloop()

最后,正如我上面提到的,matplotlib 文档清楚地显示了一个.suptitle()属性,但是,将Figure.suptitle('Data')结果添加到AttributeError: module 'matplotlib.figure' has no attribute 'suptitle'.我究竟做错了什么?

先感谢您!

python
  • 1 1 个回答
  • 27 Views

1 个回答

  • Voted
  1. Best Answer
    RuthC
    2024-05-30T02:26:57+08:002024-05-30T02:26:57+08:00

    如果您使用更标准的命名约定,一切可能不会那么混乱。

    1. 在Python中,类通常是大写的,所以我建议从模块中导入类,而不是用大写字母重命名模块
    from matplotlib.figure import Figure
    
    1. 在 Matplotlib 中,该类的特定实例Figure通常命名为fig。

    您可以在实例化图形时设置布局

    fig = Figure(figsize=(15,9), layout='tight')
    

    suptitle是图形实例上的方法

    fig.suptitle('My Title')
    

    将其放在一起:

    import customtkinter as ctk
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    
    from matplotlib.figure import Figure
    
    # create the window using CTK
    window = ctk.CTk()
    
    # create the plot of random data
    fig = Figure(figsize=(15,9), layout='tight')
    fig.suptitle('My Title')
    
    # create the subplots
    ax1 = fig.add_subplot(2, 3, 1)
    ax2 = fig.add_subplot(2, 3, 2)
    ax3 = fig.add_subplot(2, 3, 3)
    ax4 = fig.add_subplot(2, 3, 4)
    ax5 = fig.add_subplot(2, 3, 5)
    ax6 = fig.add_subplot(2, 3, 6)
    
    # plot data in each subplot
    data = [1, 2, 3, 4, 5]
    ax1.plot(data)
    ax2.plot(data)
    ax3.plot(data)
    ax4.plot(data)
    ax5.plot(data)
    ax6.plot(data)
    
    # add titles to each subplot
    ax1.set_title('Subplot 1')
    ax2.set_title('Subplot 2')
    ax3.set_title('Subplot 3')
    ax4.set_title('Subplot 4')
    ax5.set_title('Subplot 5')
    ax6.set_title('Subplot 6')
    
    # Add the plot to the window
    canvas = FigureCanvasTkAgg(fig, master=window)
    canvas.draw()
    canvas.get_tk_widget().pack(side='top', fill='both', expand=True)
    
    exit_button = ctk.CTkButton(window, text='Exit', command=window.destroy)
    exit_button.pack(side='bottom')  # add the exit button to the window
    
    window.mainloop()
    

    我明白了: 在此输入图像描述

    • 2

相关问题

  • 如何将 for 循环拆分为 3 个单独的数据框?

  • 如何检查 Pandas DataFrame 中的所有浮点列是否近似相等或接近

  • “load_dataset”如何工作,因为它没有检测示例文件?

  • 为什么 pandas.eval() 字符串比较返回 False

  • Python tkinter/ ttkboostrap dateentry 在只读状态下不起作用

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve