【发布时间】:2016-08-24 11:51:23
【问题描述】:
我正在学习numpy。我希望绘制不同温度下的普朗克定律图,因此分别有两个np.arrays、T 和l 用于温度和波长。
import scipy.constants as sc
import numpy as np
import matplotlib.pyplot as plt
lhinm = 10000 # Highest wavelength in nm
T = np.linspace(200, 1000, 10) # Temperature in K
l = np.linspace(0, lhinm*1E-9, 101) # Wavelength in m
labels = np.linspace(0, lhinm, 6) # Axis labels giving l in nm
B = (2*sc.h*sc.c**2/l[:, np.newaxis]**5)/(np.exp((sc.h*sc.c)/(T*l[:, np.newaxis]*sc.Boltzmann))-1)
for ticks in [True, False]:
plt.plot(B)
plt.xlabel("Wavelength (nm)")
if ticks:
plt.xticks(l, labels)
plt.title("With xticks()")
plt.savefig("withticks.png")
else:
plt.title("Without xticks()")
plt.savefig("withoutticks.png")
plt.show()
我想用波长 (nm) 标记 x 轴。如果我不调用plt.xitcks(),x 轴上的标签似乎是数组 B 中的索引(其中包含计算值)。
我已经看到答案 7559542,但是当我调用 plt.xticks() 时,所有值都在轴的左侧被挤压,而不是沿着它均匀分布。
那么定义我自己的一组值(在这种情况下是l 中的值的子集)并将它们放在轴上的最佳方法是什么?
【问题讨论】:
标签: python numpy matplotlib