【问题标题】:Draw several graphs in matplotlib, python在matplotlib、python中画几张图
【发布时间】:2013-12-04 12:12:52
【问题描述】:

我需要在 python matplotlib 中绘制图形。

对于每个图形,我之前都会进行一些计算,然后绘制图形。 以下是部分代码:

import matplotlib.pyplot as plt

def draw(LuxCoordinates, finalPix, verName):
    plt.axes([0.2, 0.2, 0.7, 0.6]);
    plt.xlim(0,3500); #Axis x
    plt.ylim(0,100); #Axis y
    plt.plot(LuxCoordinates, finalPix, 'g');
    plt.scatter(LuxCoordinates, finalPix, linewidths=1)
    plt.grid(axis)
    plt.xlabel('X_Coordinates', color='r');
    plt.ylabel('Y_Coordinates', color='r');
    plt.title(verName, color='#afeeee');
    savefig(verName+'.png');
    plt.show();

我的问题是我两次或多次调用此函数,根据我拥有的图表数量,我在单独的图中得到了图表,但我想在同一个图中绘制所有图表,以便比较它们. 我该怎么做? 谢谢大家!

【问题讨论】:

    标签: python graph matplotlib draw


    【解决方案1】:

    删除plt.show()

    您的函数应该获取figureaxis 作为输入参数,并在该图形上进行绘图,然后在完成所有绘图后在函数外部调用plt.show()

    这是一个最小的例子:

    import matplotlib.pyplot as plt
    import numpy as np
    
    def plotter ( ax, col ):
        data = np.random.normal( size=(50, 2 ) )
        x, y = data[:, 0], data[:, 1 ]
        ax.scatter( x, y, color=col )
    
    fig = plt.figure()
    ax = fig.add_axes([0.2, 0.2, 0.7, 0.6])
    
    plotter( ax, 'Blue' )
    plotter( ax, 'Red' )
    fig.show( )
    

    【讨论】:

    • 非常感谢,我只是删除 plt.show() 并将其放在所有对绘图函数的调用之后。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 2015-03-20
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    相关资源
    最近更新 更多