【问题标题】:Imposing one graph onto another将一张图叠加到另一张图上
【发布时间】:2017-01-24 06:36:21
【问题描述】:

我在上物理实验室课,我们必须编写一些代码来分析我们收集的一些数据。我的问题很简单而且可能很愚蠢,但我只是想知道如何使用 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()

【问题讨论】:

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

标签: python plot histogram physics gaussian


【解决方案1】:

当您调用 plt.figure() 时,您正在以不同的图形创建一个新的绘图区域。

如果您第二次不调用它,您将绘制与第一次相同的图表。

然而,这本身并不总是一个解决方案,如果它们具有非常不同的比例,则可能导致一个图被另一个图大大超出。

幸运的是,这里不是这种情况,所以我不会详细说明如何在一张图中使用 2 个不同的比例,但你可以在这里查看 (http://matplotlib.org/examples/api/two_scales.html)

通过从您的代码中注释第二个 plt.figure() 您会得到:

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

【讨论】:

    猜你喜欢
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多