【发布时间】:2016-09-22 19:52:01
【问题描述】:
我正在做一个热物理学项目,为此我需要将直方图与平滑曲线进行比较。我的问题是直方图放置在曲线的右侧(曲线通过条形的左上角是一致的):
我希望曲线穿过条形顶部的中间,就像它应该的那样。这可能看起来像一件小事,但它真的让我很生气。我希望有一个人可以帮助我!
程序如下所示:
import numpy as np
import matplotlib.pyplot as plt
#--Constants--
M = 20 # Jmax
N = 1000 # Number of J values
T = 50 # Actually T/theta_r
J1 = np.linspace(0,M,N)
J2 = np.linspace(0,M,M+1)
#--Calculate z--
def z(J):
return(2*J+1)*np.exp(-J*(J+1)/T)
#--Plot--
width = .9 #Width of columns
plt.bar(J2, z(J2), width=width) #Plotting histogram
#plt.xticks(ind + width / 2, ind) #Replacing the indexes under the columns
plt.plot(J1,z(J1),'-r', linewidth=2)
SZ={'size':'16'}
plt.title('Different terms $z(j)$ plotted as function of $j$',**SZ)
plt.xlabel('$j$',**SZ)
plt.ylabel('$z(j)$',**SZ)
plt.show()
【问题讨论】:
-
我猜这个“问题”与酒吧的放置方式有关!增加粒度并绘制更多条形图,您会看到整个图片都发生了变化,例如试试
width = .7?进一步说明:用于绘制单个条形图的数据分布不均;根据.9-范围内数据的“内部”峰值,您绘制的曲线并不完美地位于条形的中间...
标签: python matplotlib histogram