How should I pass a matplotlib object through a function; as Axis, Axes or Figure?

前端 未结 1 1318
栀梦
栀梦 2021-02-01 10:58

Sorry in advance if this is a little long winded but if I cut it down too much the problem is lost. I am trying to make a module on top of pandas and matplotlib which will give

1条回答
  •  清歌不尽
    2021-02-01 11:20

    You should pass around Axes objects and break your functions up to operate on a single axes at a time. You are close, but just change

    import numpy as np
    import matplotlib.pyplot as plt
    
    def _profile(ax, x, y):
        ln, = ax.plot(x, y)
        # return the Artist created
        return ln
    
    
    def profile_matrix(n, m):
        fig, ax_array = plt.subplots(n, m, sharex=True, sharey=True)
        for ax in np.ravel(ax_array):
            _profile(ax, np.arange(50), np.random.rand(50))
    
    profile_matrix(3, 3)
    

    enter image description here

    0 讨论(0)
提交回复
热议问题