2017-01-24 2 views
0

私は物理ラボのクラスで、収集したデータを分析するためのコードを書く必要があります。私の質問は単純で、おそらくばかげていますが、私は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

"上にある"とはどういう意味ですか?同じ座標系の中か、同じ画像/ Figureウィンドウのサブプロットですか? – LutzL

+0

[こちら](http://support.softwarefx.com/media/61d7eaea-4685-e311-84a5-0019b9e6b500/large) –

+0

[This]は、ガウスベルカーブを背景とするヒストグラムです。 'plt.show'は現在のプロットキャンバスをFigureウィンドウに表示し、将来の描画操作のために新しいキャンバスを作成します。 – LutzL

答えて

0

あなたがplt.figure()を呼び出すと、別の図で新しいプロットエリアが作成されます。

もう一度呼びたくない場合は、最初のグラフと同じグラフにプロットします。

ただし、非常にスケールが異なる場合、グラフが他のグラフによって大規模にスケールされる可能性があります。幸い

ここではそのない場合、私は文句を言わない1つのグラフに2つの異なるスケールを使用する方法の詳細に入るが、あなたはから)(第2 plt.figureをコメントにし、ここで(http://matplotlib.org/examples/api/two_scales.html

それを確認することができますので、あなたのコードは、あなたがこれを取得:

enter image description here

はそれが役に立てば幸い! ps:次にmatplotlibタグをつけて投稿すると、基本的にはmatplotlibの質問があったので、physicsよりも速い応答が得られます。

関連する問題