簡體   English   中英

縮放Y和X軸python圖

[英]Scaling Y and X axis python graph

我有一組數據,並通過使用它們制作了一個圖形。 問題是數據看起來不正確縮放,因為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()                  

您可以使用plt.xlim([-10,0])plt.ylim([-10,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")

我還可以將較低的值作為插入放大

# 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()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM