我有一组数据,并使用它们制作了一个图形。问题是数据看起来不正确缩放,因为y轴的范围是0到30000,而x轴的范围是-2到30。如何解决此问题?谢谢
这是我的代码,
import numpy as np
import matplotlib.pyplot as plt
voltage365nm = [-1.877,-2.0,-1.5,-1.0,0.0,5.0,10.0,20.0,30.0]
voltage405nm = [-1.437,-2.0,-1.5,-1.0,0.0,5.0,10.0,20.0,30.0]
voltage546nm = [-0.768,-2.0,-1.5,-1.0,0.0,5.0,10.0,20.0,30.0]
current365nm = [0.0,5.6,151.1,428,1164,5760,9870,1626,20700]
current405nm = [0.0,-8.2,-2.6,70.2,278,1954,2460,3970,5021]
current546nm = [0.0,-6.5,-6.1,-5.4,248,1435,2240,3250,3750] plt.plot(voltage365nm,current405nm,"r--",marker="s",label="$\lambda$=365 nm")
plt.plot(voltage405nm,current405nm,"b-.",marker="o",label="$\lambda$=405nm")
plt.plot(voltage546nm,current546nm,"g-",marker="^",label="$\lambda$=546nm")
plt.legend(loc='best')
plt.xlabel("Voltage (V)")
plt.ylabel("Current (I x $10^{-13}A}$)")
plt.title("Current vs Voltage")
plt.grid(b=True, which='major', color='g', linestyle='--')
plt.grid(b=True, which='minor', color='r', linestyle='--', alpha=0.2)
plt.show()
答案 0 :(得分:0)
您可以使用plt.xlim([-10,0])
和plt.ylim([-10,0])
来指定轴的最小值和最大值。
答案 1 :(得分:0)
我运行了您的代码,并得到以下图表:
您是否担心y轴下端的数据点被“压缩”了?如果真是这样,也许在对数轴上绘图可能会有所帮助。像这样使用set_yscale('log')
方法:
ax = plt.gca()
ax.set_yscale('log')
有了这个,我得到以下图表:
当然,问题在于某些y轴值为负,因此无法直接在对数刻度上绘制。完整的解决方案是在所有电流上加上一个常数,使它们为正。
PS-我认为您的plt.plot
命令之一存在错误:
plt.plot(voltage365nm,current405nm,"r--",marker="s",label="$\lambda$=365 nm")
应该是
plt.plot(voltage365nm, current365nm,"r--",marker="s",label="$\lambda$=365 nm")
答案 2 :(得分:0)
我还会将较低的值作为放大
插入# your existing code before plt.show()
left, bottom, width, height = [0.32, 0.55, 0.4, 0.3]
ax2 = fig.add_axes([left, bottom, width, height])
ax2.plot(voltage365nm,current365nm,"r--",marker="s",label="$\lambda$=365 nm")
ax2.plot(voltage405nm,current405nm,"b-.",marker="o",label="$\lambda$=405nm")
ax2.plot(voltage546nm,current546nm,"g-",marker="^",label="$\lambda$=546nm")
ax2.set_xlim(-2.1, 1)
ax2.set_ylim(-100, 1500)
plt.grid(b=True, which='major', color='g', linestyle='--')
plt.show()