我在物理实验课上,我们必须编写一些代码来分析我们收集的一些数据。我的问题很简单,可能很愚蠢,但我只是想知道如何使用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()
答案 0 :(得分:0)
当你打电话给plt.figure()时,你正在以不同的数字制作一个新的情节区域。
如果你第二次不打电话,你会在第一张图中绘制。
然而,如果它们具有非常不同的尺度,那么它本身并不总是一种解决方案,这可能导致一个图形被另一个图形大量超越。幸运的是,这里并非如此,所以我不会详细介绍如何在一个图表中使用2个不同的比例,但你可以在这里查看(http://matplotlib.org/examples/api/two_scales.html)
通过从代码中注释第二个plt.figure(),你得到了这个:
希望它有所帮助! ps:下次尝试使用matplotlib标签发布时,它会得到比物理更快的响应,因为它基本上是你的matplotlib问题。