Python与matplotlib - 重用绘图功能

asp*_*ade 11 python matplotlib

我对这个问题有一个跟进问题.

通过使用多个python脚本可以在图的不同部分工作,是否可以简化图形生成?

例如,如果我有以下功能:

FunctionA:绘制一个直方图
FunctionB:在其中绘制一个带有文本的框
FunctionC:绘制一个C
的图形函数:绘制一个D的图形

如何在不同的脚本中重用上述功能?例如,如果我想创建一个带有C图形的直方图的图形,我会以某种方式从我的脚本中调用FunctionA和FunctionC.或者,如果我想要一个带有两个图的图形,我会调用FunctionC和FunctionD.

我不确定我是否在清楚地解释自己,但另一种问这个问题的方法是:如何将一个图形对象传递给一个函数,然后让函数在传递的图形对象上绘制一些东西,然后将其返回到主脚本添加其他东西,如标题或东西?

tom*_*m10 8

在这里,您希望使用Artist对象,并根据需要将它们传递给函数:

import numpy as np
import matplotlib.pyplot as plt

def myhist(ax, color):
    ax.hist(np.log(np.arange(1, 10, .1)), facecolor=color)

def say_something(ax, words):
    t = ax.text(.2, 20., words)
    make_a_dim_yellow_bbox(t)

def make_a_dim_yellow_bbox(txt):
    txt.set_bbox(dict(facecolor='yellow', alpha=.2))

fig = plt.figure()
ax0 = fig.add_subplot(1,2,1)
ax1 = fig.add_subplot(1,2,2)

myhist(ax0, 'blue')
myhist(ax1, 'green')

say_something(ax0, 'this is the blue plot')
say_something(ax1, 'this is the green plot')

plt.show()
Run Code Online (Sandbox Code Playgroud)

alt text http://i28.tinypic.com/2i6f7f5.png