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
    • 最新
    • 标签
主页 / user-7525978

ohshitgorillas's questions

Martin Hope
ohshitgorillas
Asked: 2024-09-28 00:29:25 +0800 CST

rcparams 未应用于自定义 matplotlib 类

  • 7

我正在尝试编写一个自定义图形类,matplotlib.figure.Figure除其他功能外,它还会自动应用正确的格式。这是当前配置:

import matplotlib
from matplotlib.axes import Axes
from matplotlib.figure import Figure
from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as Canvas

class CustomFigure(Figure):
    def __init__(self, figsize: tuple, layout: str):
        super().__init__(figsize=figsize, layout=layout)
        self.canvas = Canvas(self)
        matplotlib.use("QtAgg")
        self.set_common_params()
        
        
    def generate_axes(self, num_axes: int, layout: tuple = None) -> Axes:
        if layout is None:
            layout = (1, num_axes)
        return self.subplots(*layout)
    
        
    def set_common_params(self):
        matplotlib.rcParams["figure.titlesize"] = 45
        matplotlib.rcParams["axes.titlesize"]   = 13
        matplotlib.rcParams["axes.labelsize"]   = 12
        matplotlib.rcParams["axes.linewidth"]   = 1.5
        matplotlib.rcParams["xtick.labelsize"]  = 11
        matplotlib.rcParams["ytick.labelsize"]  = 11
        
    
    @staticmethod
    def set_labels(ax: Axes, xlabel: str, ylabel: str, title: str = None):
        ax.set_xlabel(xlabel)
        ax.set_ylabel(ylabel)
        if title is not None:
            ax.set_title(title)
            
    
    def generate_pdf(self, filename: str):
        self.savefig(f"{filename}.pdf")
        
        


if __name__ == "__main__":
    import sys
    from PySide6.QtWidgets import QApplication, QMainWindow
    app = QApplication(sys.argv)
    win = QMainWindow()
    fig = CustomFigure((5,5), "tight")
    fig.set_labels(fig.generate_axes(1), "X", "Y", "Title")
    win.setCentralWidget(fig.canvas)
    win.show()
    sys.exit(app.exec())

我尝试将 rcParams 放在代码的每个可能的位置,甚至在类定义之前,但没有任何效果。

如何正确应用 rcParams?

python
  • 1 个回答
  • 23 Views
Martin Hope
ohshitgorillas
Asked: 2024-05-30 01:42:26 +0800 CST

Matplotlib布局问题

  • 5

我正在用 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 个回答
  • 27 Views
Martin Hope
ohshitgorillas
Asked: 2024-05-29 15:27:32 +0800 CST

尝试关闭简单 ctk 窗口时出错

  • 5

我有一个非常简单的数据窗口,我试图显示它并提供退出按钮:

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

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

# create the plot of random data
data = [1, 2, 3, 4, 5]
plt.plot(data)

# Add the plot to the window
canvas = FigureCanvasTkAgg(plt.gcf(), 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()

问题是,单击“退出”后,当窗口消失时,终端继续在后台运行,并出现以下错误:

invalid command name "2607166565512update"
    while executing
"2607166565512update"
    ("after" script)
invalid command name "2607167621640check_dpi_scaling"
    while executing
"2607167621640check_dpi_scaling"
    ("after" script)
invalid command name "2607249791944_click_animation"
    while executing
"2607249791944_click_animation"
    ("after" script)

我做错了什么以及如何让“退出”完全关闭程序?

python
  • 1 个回答
  • 24 Views
Martin Hope
ohshitgorillas
Asked: 2024-05-26 11:22:54 +0800 CST

如何防止文本框切入 CTkFrame 的边框?

  • 6

我正在尝试使用 customtkinter 创建一个统计框架,但遇到了左对齐文本切入边框的问题:

import customtkinter as ctk

window = ctk.CTk()

ctk.set_appearance_mode('light')

main_frame = ctk.CTkFrame(window, fg_color='white')
main_frame.pack(fill=ctk.BOTH, expand=True)
text_frame = ctk.CTkFrame(main_frame, fg_color='white', border_color='black', border_width=2)
text_frame.pack_propagate(False)
text_frame.pack(side=ctk.TOP)

# add a hello world label
hello_label = ctk.CTkLabel(text_frame, text="Hello, World!", font=('TkDefaultFont', 24), anchor='w', justify='left')
hello_label.pack(fill='x')

hello_label = ctk.CTkLabel(text_frame, text="Hello, World!", font=('TkDefaultFont', 24), anchor='w', justify='left')
hello_label.pack(fill='x')

hello_label = ctk.CTkLabel(text_frame, text="Hello, World!", font=('TkDefaultFont', 24), anchor='w', justify='left')
hello_label.pack(fill='x')


window.mainloop()

如何在几行左对齐文本周围绘制边框而不让文本切入边框?

python
  • 1 个回答
  • 17 Views

Sidebar

Stats

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

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

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

    • 1 个回答
  • Marko Smith

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

    • 1 个回答
  • Marko Smith

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

    • 6 个回答
  • Marko Smith

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

    • 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 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +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

热门标签

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