matplotlib 复制图形并应用更改 [英] matplotlib duplicate figure and apply changes

查看:23
本文介绍了matplotlib 复制图形并应用更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一系列具有相同布局的图形.由于布局要花费很多行代码,因此我试图复制第一个完成的代码,并仅更改一些内容(例如标记),并在可能的情况下更改数据,尽管我知道这样做可能会使事情复杂化,因为可能需要重新缩放和因此,在"plt.show()"命令的末尾,将显示原始图形以及重复的图形.

I'm making a series of figures with the same layout. As the layout is taking many lines of codes, I'm trying to duplicate the first once done and change only a few things, such as markers, and if possible the data though I know it might complicate things as it would need probably rescaling and so that at the end at 'plt.show()' command, the original figure plus the duplicated get displayed.

推荐答案

我不确定您描述的复制设置"是否真的可行……它可能需要对所有布局中涉及的对象(轴、轴对象、线对象、补丁对象等)我可能完全错误,但这是我的直觉.我在这里放置了一些可能接近您想要的东西的东西,因为不必重复很多布局规范.

I'm not sure that "duplicating settings" in the way you describe is really feasible ... it probably requires lots of low-level access of all the objects involved in the layout (the axes, the axis objects, the line objects, patch objects, etc.) I could be totally wrong about that, but that's my instinct. I've put here something which may do close to what you want, though, as in not having to duplicate a lot of layout specifications.

import matplotlib.pyplot as plt
import numpy as np

def make_layout(data):
    fig = plt.figure()

    ax = fig.add_subplot(111)

    p, = ax.plot(data,'o')

    p.set_markerfacecolor('green')
    # Presumably lots of complicated settings here

    return fig, ax, p

data = np.linspace(0,1)

f1, a1, p1 = make_layout(data)
f2, a2, p2 = make_layout(data**2)

# Make the tweaks you want
a2.set(axis_bgcolor='m')
p2.set_markerfacecolor('yellow')

这篇关于matplotlib 复制图形并应用更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆