Python 把一个图形加在另一个图形上

Python 把一个图形加在另一个图形上,python,plot,histogram,physics,gaussian,Python,Plot,Histogram,Physics,Gaussian,我在上物理实验课,我们必须写一些代码来分析我们收集的数据。我的问题很简单,可能很愚蠢,但我只是想知道如何使用python在另一个图形上绘制一个图形。这是我到目前为止的代码谢谢 %pylab import numpy as np import matplotlib.mlab as mlab import matplotlib.pyplot as plt #SIGNAL DATA dataSig = [658, 679, 683, 691, 693, 693, 695, 696, 696, 696

我在上物理实验课,我们必须写一些代码来分析我们收集的数据。我的问题很简单,可能很愚蠢,但我只是想知道如何使用python在另一个图形上绘制一个图形。这是我到目前为止的代码谢谢

%pylab
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

#SIGNAL DATA
dataSig = [658, 679, 683, 691, 693, 693, 695, 696, 696, 696, 697, 699, 699, 700, 700, 700, 702, 703, 703, 704, 706, 706, 708, 708, 709, 709, 712, 712, 713, 714, 714, 715, 715, 715, 716, 716, 716, 717, 717, 717, 718, 718, 718, 718, 719, 720, 720, 721, 721, 721, 722, 723, 723, 724, 725, 725, 725, 726, 726, 726, 727, 727, 728, 728, 729, 730, 730, 731, 731, 731, 731, 732, 732, 733, 734, 734, 734, 734, 735, 736, 737, 738, 738, 738, 738, 740, 740, 741, 741, 741, 742, 743, 743, 743, 743, 743, 743, 743, 744, 744, 745, 746, 746, 746, 746, 747, 747, 747, 747, 748, 749, 749, 750, 750, 750, 750, 751, 751, 751, 751, 752, 752, 752, 754, 754, 756, 756, 757, 757, 757, 759, 759, 760, 760, 760, 762, 762, 762, 762, 762, 762, 763, 764, 765, 765, 765, 765, 766, 766, 766, 767, 767, 768, 769, 769, 770, 770, 771, 773, 775, 776, 780, 786, 786, 786, 787, 790, 790, 793, 796, 797, 798, 817, 823]
#[658,679,683,691,693,695,696,697,699,700,702,703,704,706,708,709,712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,729,730,731,732,733,734,735,736,737,738,740,741,742,743,744,745,746,747,748,749,750,751,752,754,756,757,759,760,762,763,764,765,766,767,768,769,770,771,773,775,776,780,786,787,790,793,796,797,798,817,823] #[1,1,1,1,1,1,3,1,2,3,1,2,1,2,2,1,2,1,2,3,2,3,3,1,2,3,1,2,1,3,3,2,2,1,1,4,2,1,4,1,1,1,4,2,3,1,7,2,1,4,4,1,2,4,4,3,2,2,2,2,3,6,1,1,4,3,2,1,2,2,1,1,1,1,1,3,1,2,1,1,1,1,1,1]

#SIGNAL DEFINED VARIABLES
ntestpoints = 175
themean = 739.1
#sigma = ?
#amp = center/guassian

#SIGNAL GAUSSIAN FITTING FUNCTION
def mygauss(x, amp, center, sigma):
    """This is an example gaussian function, which takes in x values, the amplitude (amp),
    the center x value (center) and the sigma of the Gaussian, and returns the respective y values."""
    y = amp * np.exp(-.5*((x-center)/sigma)**2)
    return y

#SIGNAL PLOT, NO GAUSS
plt.figure(figsize=(10,6))
plt.hist(dataSig,bins=ntestpoints/10,histtype="stepfilled",alpha=.5,color='g',range=[600,900])
plt.xlabel('Number of Counts/Second',fontsize=20)
plt.ylabel('Number of Measurements',fontsize=20)
plt.title('Measured Signal Count Rate Fitting with Gaussian Function',fontsize=22)
plt.axvline(themean,linestyle='-',color='r')
#plt.axvline(themean+error_on_mean,linestyle='--',color='b')
#plt.axvline(themean-error_on_mean,linestyle='--',color='b')
#plt.axvline(testmean,color='k',linestyle='-')
plt.show()

#------------------------------------------------------------

# define a function to make a gaussian with input values, used later
def mygauss(x, amp, center, sigma):
    """This is an example gaussian function, which takes in x values, the amplitude (amp),
    the center x value (center) and the sigma of the Gaussian, and returns the respective y values."""
    y = amp * np.exp(-.5*((x-center)/sigma)**2)
    return y

npts = 40   # the number of points on the x axis
x = np.linspace(600,900,npts)    # make a series of npts linearly spaced values between 0 and 10
amp = 40
center = 740.5
sigma = 40
y = mygauss(x, amp, center, sigma)
print y

plt.figure(figsize=(10,6))
plt.plot(x,y,'bo', label='data points')     
plt.text(center, amp, "<-- peak is here",fontsize=16)     # places text at any x/y location on the graph
plt.xlabel('X axis',fontsize=20)
plt.ylabel('Y axis', fontsize=20)
plt.title('A gaussian plot \n with some extras!',fontsize=20)
plt.legend(loc='best')
plt.show()
%pylab
将numpy作为np导入
将matplotlib.mlab导入为mlab
将matplotlib.pyplot作为plt导入
#信号数据
数据集=[658, 679, 683, 691, 693, 693, 695, 696, 696, 696, 697, 699, 699, 700, 700, 700, 702, 703, 703, 704, 706, 706, 708, 708, 709, 709, 712, 712, 713, 714, 714, 715, 715, 715, 716, 716, 716, 717, 717, 717, 718, 718, 718, 718, 719, 720, 720, 721, 721, 721, 722, 723, 723, 724, 725, 725, 725, 726, 726, 726, 727, 727, 728, 728, 729, 730, 730, 731, 731, 731, 731, 732, 732, 733, 734, 734, 734, 734, 735, 736, 737, 738, 738, 738, 738, 740, 740, 741, 741, 741, 742, 743, 743, 743, 743, 743, 743, 743, 744, 744, 745, 746, 746, 746, 746, 747, 747, 747, 747, 748, 749, 749, 750, 750, 750, 750, 751, 751, 751, 751, 752, 752, 752, 754, 754, 756, 756, 757, 757, 757, 759, 759, 760, 760, 760, 762, 762, 762, 762, 762, 762, 763, 764, 765, 765, 765, 765, 766, 766, 766, 767, 767, 768, 769, 769, 770, 770, 771, 773, 775, 776, 780, 786, 786, 786, 787, 790, 790, 793, 796, 797, 798, 817, 823]
#[658,679,683,691,693,695,696,697,699,700,702,703,704,706,708,709,712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,729,730,731,732,733,734,735,736,737,738,740,741,742,743,744,745,746,747,748,749,750,751,752,754,756,757,759,760,762,763,764,765,766,767,768,769,770,771,773,775,776,780,786,787,790,793,796,797,798,817,823] #[1,1,1,1,1,1,3,1,2,3,1,2,1,2,2,1,2,1,2,3,2,3,3,1,2,3,1,2,1,3,3,2,2,1,1,4,2,1,4,1,1,1,4,2,3,1,7,2,1,4,4,1,2,4,4,3,2,2,2,2,3,6,1,1,4,3,2,1,2,2,1,1,1,1,1,3,1,2,1,1,1,1,1,1]
#信号定义变量
ntestpoints=175
平均值=739.1
#西格玛=?
#amp=中心/瓜西安
#信号高斯拟合函数
def mygauss(x,amp,center,sigma):
“”“这是一个示例高斯函数,它采用x值,振幅(amp),
高斯函数的中心x值(中心)和sigma,并返回相应的y值
y=amp*np.exp(-0.5*((x-中心)/sigma)**2)
返回y
#信号图,无高斯
plt.图(figsize=(10,6))
plt.hist(dataSig,bin=ntestpoints/10,histtype=“stepfilled”,alpha=.5,color='g',range=[600900])
plt.xlabel('每秒计数数',fontsize=20)
plt.ylabel('测量次数',fontsize=20)
plt.title(‘测量信号计数率与高斯函数拟合’,fontsize=22)
打印轴(平均值,线型='-',颜色='r')
#plt.axvline(平均值+误差,线型='--',颜色='b')
#plt.axvline(平均误差,线型='--',颜色='b')
#plt.axvline(testmean,color='k',linestyle='-')
plt.show()
#------------------------------------------------------------
#定义一个函数,使用输入值生成高斯函数,稍后使用
def mygauss(x,amp,center,sigma):
“”“这是一个示例高斯函数,它采用x值,振幅(amp),
高斯函数的中心x值(中心)和sigma,并返回相应的y值
y=amp*np.exp(-0.5*((x-中心)/sigma)**2)
返回y
npts=40#x轴上的点数
x=np.linspace(600900,npts)#使一系列npts的值在0到10之间线性间隔
amp=40
中心=740.5
西格玛=40
y=mygauss(x,安培,中心,西格玛)
打印y
plt.图(figsize=(10,6))
plt.plot(x,y,'bo',label='data points')

当您调用plt.figure()时,您正在另一个图中创建一个新的绘图区域

如果您第二次不调用它,您将在第一个图形中绘制相同的图形

然而,这本身并不总是一个解决方案,如果它们具有非常不同的比例,那么可能会导致一个图形被另一个图形大幅放大

幸运的是,这里的情况并非如此,因此我不会详细介绍如何在一个图中使用两种不同的比例,但您可以在这里查看()

通过注释代码中的第二个plt.figure(),可以得到以下结果:

希望有帮助!
ps:下次尝试用matplotlib标签发布时,它会得到比物理更快的响应,因为这基本上是一个matplotlib问题。

上面的“是什么意思?在同一坐标系内还是作为同一图片/图形窗口中的子图?像[这个]()[这个]是以Gaußbell曲线为背景的直方图。
plt.show
在图形窗口中显示当前绘图画布,并为将来的绘图操作创建新画布。