简体   繁体   English

在matplotlib,Python中绘制几个图形

[英]Draw several graphs in matplotlib, python

I need to draw graphs in python matplotlib. 我需要在python matplotlib中绘制图形。

For each graph I do some calculates before and then draw the graphe. 对于每个图形,我都会先进行一些计算,然后绘制图形。 Here is some of the code: 这是一些代码:

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();

My problem is that I call to this function twice or more, according to the number of graphs I have, I get the graphs in separate plots, but I want to draw all graphs on the same plot, in order to compare them. 我的问题是,根据我拥有的图形数量,我两次或多次调用此函数,得到的图形分别位于不同的图形中,但是我想在同一图形上绘制所有图形,以便进行比较。 How can I do it? 我该怎么做? Thank you all! 谢谢你们!

remove plt.show() 删除plt.show()

your function should get a figure or axis as an input parameter, and do the plotting on that figure, and then outside the function when you have done all the plotting you may call plt.show() 您的函数应该获取figureaxis作为输入参数,并在该图形上进行绘制,然后在完成所有绘制后在函数外部进行绘制,您可以调用plt.show()

here is a minimal example: 这是一个最小的示例:

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( )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM