簡體   English   中英

Matplotlib 為什么x軸上的范圍不對?

[英]Matplotlib why is the range on the x-axis wrong?

如何使第二個 plot 介於 -4800 和 4800 之間? 我試過的東西因為一些荒謬的原因不起作用......

import math
import numpy as np
import matplotlib.pyplot as plt

f1 = np.arange(0, 9600, 1)
f2 = np.arange(-4800, 4800, 1)

def H1(f):
    w = 2*math.pi*f
    out = 1/(1+np.complex(0, 0.00006631455*w)) # required cut-off frequency of 2400 Hz
    return out

def magH1(f):
    out = np.absolute(H1(f))
    return out

def phaseH1(f):
    out = np.angle(H1(f))
    return out


magnitude = np.vectorize(magH1)
phase = np.vectorize(phaseH1)

plt.subplot(1, 2, 1)
plt.xticks(np.arange(0, 9600, 1200))
plt.xlabel('frequency (Hz)')
plt.ylabel('gain (V)')
plt.grid(True)
plt.plot(magnitude(f1))
plt.subplot(1, 2, 2)
plt.xticks(np.arange(-4800, 4800, 1200))
plt.xlabel('frequency (Hz)')
plt.ylabel('phase (radians)')
plt.grid(True)
plt.plot(phase(f2))
plt.show()

劇情形象

如您所見,我突出顯示了問題所在的紅色區域,由於某些奇怪的原因,范圍不正確。 對於這個可怕的問題,我真的很感激能得到一些幫助。

我知道你不應該在問題結束時說謝謝,但你所有的支持讓我感到驚訝。

繪圖時最好指定x值。 這是一個清理過的版本:

fig, axs = plt.subplots(ncols=2, figsize=(10, 5))

axs[0].set_xticks(np.arange(0, 9600, 1200))
axs[0].set_xlabel('frequency (Hz)')
axs[0].set_ylabel('gain (V)')
axs[0].grid(True)
axs[0].plot(f1, magnitude(f1))

axs[1].set_xticks(np.arange(-4800, 4800, 1200))
axs[1].set_xlabel('frequency (Hz)')
axs[1].set_ylabel('phase (radians)')
axs[1].grid(True)
axs[1].plot(f2, phase(f2))

fig.tight_layout()
plt.show()

Output:

在此處輸入圖像描述

暫無
暫無

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

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