2017-09-10 70 views
-1

matplotlib中有沒有任何方法來保持勾選位置均勻,而保持它們的值不均勻,以便數據可能擠壓一些間隔,並可能在另一個間隔上展開。 例如下面的代碼生成與蜱正弦波[0.0,0.5,1.0,1.5,2.0]matplotlib - 均勻保持勾號位置,但值不均勻

import matplotlib.pyplot as plt 
import numpy as np 
t = np.arange(0.0, 2.0, 0.01) 
s = 1 + np.sin(2*np.pi*t) 
plt.plot(t, s) 
plt.xlabel('time (s)') 
plt.ylabel('voltage (mV)') 
plt.title('About as simple as it gets, folks') 
plt.grid(True) 
ax = plt.gca() 
ax.get_xaxis().get_major_formatter().set_useOffset(False) 
plt.autoscale(False) 
ax.xaxis.set_ticks([0.0,0.5,1.0,1.5,2.0]) 
plt.show() 

我想在ax.xaxis.set_ticks爲值0.5變爲0.25([0.0,0.5,1.0 ,1.5,2.0]),但保持在圖上的相同位置。

+0

您需要提供至少一個你想要達到什麼樣的手繪製樣機。正如下面的答案所示,你想要的是完全不清楚的。 –

+0

這是https://stackoverflow.com/questions/32185411/break-in-x-axis-of-matplotlib你在找什麼?在x軸的1/4處繪製0-0.25,在剩餘的3/4處從0.25-2.0繪製? –

回答

0

顯然以下不是OP所要求的。我會把它留在這裏,直到問題被編輯,這樣人們至少明白什麼是不想要的。您可以添加set_ticklabels以不同方式標記刻度。

ax.xaxis.set_ticks( [0.0, 0.50, 1.0,1.5,2.0]) 
ax.xaxis.set_ticklabels([0.0, 0.25, 1.0,1.5,2.0]) 

Comlpete例如:

import matplotlib.pyplot as plt 
import numpy as np 
t = np.arange(0.0, 2.0, 0.01) 
s = 1 + np.sin(2*np.pi*t) 
plt.plot(t, s) 
plt.xlabel('time (s)') 
plt.ylabel('voltage (mV)') 
plt.title('About as simple as it gets, folks') 
plt.grid(True) 
ax = plt.gca() 
ax.get_xaxis().get_major_formatter().set_useOffset(False) 
plt.autoscale(False) 
ax.xaxis.set_ticks([0.0,0.5,1.0,1.5,2.0]) 
ax.xaxis.set_ticklabels([0.0,0.25,1.0,1.5,2.0]) 
plt.show() 

enter image description here

+0

我的意圖不是更改標籤,而是根據刻度值僅繪製標記的值和預期的數據。 – ERD

+0

這並不完全清楚。應該在哪裏0.4或0.6位於?你會想解釋一下你想在這個問題上做更多細節。 – ImportanceOfBeingErnest

+0

實際上,我想要實現的是,當更改刻度值時,我希望matplotlib不要更改刻度的位置,而不是改變它繪製數據的方式。例如,對於正弦波,我想用0.25來改變數值0.5,並且希望保持該值代替0.5。通過這樣做,我希望matplotlib根據tick間隔而不是自動調整來繪製正弦波。 – ERD