【问题标题】:Creating multiplots in Python from function outcomes根据函数结果在 Python 中创建多图
【发布时间】:2021-03-02 02:05:35
【问题描述】:

我有一个函数可以在给定字典的情况下创建复杂的绘图。现在我有两个字典,我想创建一个多图。直觉会说:

import numpy as np
import matplotlib.pyplot as plt

dict_1={'m':1,'n':0}
dict_2={'m':5,'n':1}

def nice_plot(dict):
  x=np.linspace(-5,5)
  _,ax=plt.subplots()
  return ax.plot(x,dict['m']*x+dict['n'])

fig,ax=plt.subplots(ncols=2)
ax[0]=nice_plot(dict_1)
ax[1]=nice_plot(dict_2)
fig.show()

但它不起作用,它创建一个包含 2 列的空多图,然后绘制两条线。 使用 return plt.plot(x,dict['m']*x+dict['n']) 在第二个轴上绘制两条线。

谢谢!

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    IIUC,类似于以下内容:

    def nice_plot(dict, ax):
      x=np.linspace(-5,5)
      return ax.plot(x,dict['m']*x+dict['n'])
    
    fig, ax = plt.subplots(ncols=2)
    
    nice_plot(dict_1, ax[0])
    nice_plot(dict_2, ax[1])
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 2019-08-01
      相关资源
      最近更新 更多