【问题标题】:How to add clipboard support to Matplotlib figures?如何为 Matplotlib 图形添加剪贴板支持?
【发布时间】:2015-10-14 22:40:44
【问题描述】:

在 MATLAB 中,有一个非常方便的选项可以将当前图形复制到剪贴板。尽管 Python/numpy/scipy/matplotlib 是 MATLAB 的绝佳替代品,但遗憾的是缺少这样的选项。

这个选项可以很容易地添加到 Matplotlib 图形中吗?最好是,所有 MPL 人物都应该自动受益于此功能。

我正在使用 MPL 的 Qt4Agg 后端和 PySide。

【问题讨论】:

    标签: python matplotlib plot scipy clipboard


    【解决方案1】:

    基于EelkeSpaak's answer 中描述的解决方案,我们可以编写一个函数来完成这项工作,而不是猴子修补matplotlib 的Figure 类:

    import io
    import matplotlib.pyplot as plt
    import numpy as np
    
    from PyQt5.QtGui import QImage
    from PyQt5.QtWidgets import QApplication
    
    # Example figure.
    fig, ax = plt.subplots()
    X = np.linspace(0, 2*np.pi)
    Y = np.sin(X)
    ax.plot(X, Y)
    
    def add_figure_to_clipboard(event):
        if event.key == "ctrl+c":
           with io.BytesIO() as buffer:
                fig.savefig(buffer)
                QApplication.clipboard().setImage(QImage.fromData(buffer.getvalue()))
    
    fig.canvas.mpl_connect('key_press_event', add_figure_to_clipboard)
    

    请注意,此变体使用 Qt5。

    【讨论】:

      【解决方案2】:

      最后一条评论很有用。

      1. 安装包

        pip install addcopyfighandler

      2. 导入matplotlib后导入模块,例如:

        import matplotlib.pyplot as plt
        import matplotlib.font_manager as fm
        from matplotlib.cm import get_cmap
        import addcopyfighandler

      3. 使用ctr + C将图形复制到剪贴板

      然后享受。

      【讨论】:

        【解决方案3】:

        EelkeSpaak 的解决方案包含在一个不错的模块中: addcopyfighandler

        只需pip install addcopyfighandler安装,导入matplotlib或pyplot后导入模块即可。

        【讨论】:

        【解决方案4】:

        是的,可以。想法是用一个自定义的plt.figure 替换默认的plt.figure(一种称为monkey patching 的技术),该技术注入一个用于复制到剪贴板的键盘处理程序。以下代码将允许您通过按 Ctrl+C 将任何 MPL 图复制到剪贴板:

        import io
        import matplotlib.pyplot as plt
        from PySide.QtGui import QApplication, QImage
        
        def add_clipboard_to_figures():
            # use monkey-patching to replace the original plt.figure() function with
            # our own, which supports clipboard-copying
            oldfig = plt.figure
        
            def newfig(*args, **kwargs):
                fig = oldfig(*args, **kwargs)
                def clipboard_handler(event):
                    if event.key == 'ctrl+c':
                        # store the image in a buffer using savefig(), this has the
                        # advantage of applying all the default savefig parameters
                        # such as background color; those would be ignored if you simply
                        # grab the canvas using Qt
                        buf = io.BytesIO()
                        fig.savefig(buf)
                        QApplication.clipboard().setImage(QImage.fromData(buf.getvalue()))
                        buf.close()
        
                fig.canvas.mpl_connect('key_press_event', clipboard_handler)
                return fig
        
            plt.figure = newfig
        
        add_clipboard_to_figures()
        

        请注意,如果您想使用from matplotlib.pyplot import *(例如在交互式会话中),您需要在执行上述代码之后这样做,否则您导入的figure默认命名空间将是未打补丁的版本。

        【讨论】:

        • 对我来说,我似乎无法选择fig.savefig(buf, format='svg') 的格式。我得到的图像仍然是位图图像。我在 win 7 64 上使用 python 36 和 matplotlib 2.2.2,Qt4Agg 后端。
        • 我认为这是因为 QImage 将其渲染为位图。那么真正的问题是,如何复制粘贴 svg 等矢量格式的图片?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-13
        • 2013-07-14
        • 1970-01-01
        • 1970-01-01
        • 2019-04-07
        相关资源
        最近更新 更多