gpt4 book ai didi

python - 在matplotlib、python中画几个图

转载 作者:行者123 更新时间:2023-11-28 22:52:33 25 4
gpt4 key购买 nike

我需要在 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();

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

最佳答案

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

关于python - 在matplotlib、python中画几个图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20375062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com