2012-04-18 60 views
4

是否可以將連接線繪製到matplotlib中日誌範圍內y值爲零的點?使用matplotlib將連接線繪製爲座標爲零的座標點?

我有一些數據,我想繪製在y軸上的對數刻度。一些數據的y值爲零。我意識到matplotlib不可能在對數刻度上繪製這些點,但我真的希望它能繪製從前一點或下一點(如果任一個都不爲零)的連線。

一個解決方案是簡單地用一些TINY數替換所有的零。我寧願不這樣做。

什麼matplotlib得出: Log Plot with No Connecting Line

我想什麼它來繪製: Log Plot with Connecting Line

回答

3

我會尋找使用「symlog」選項在y軸上,而不是解決這個'log'。有那麼linthreshy ARG它可以讓你指定

"The range within which the plot is linear (to avoid having the plot go to infinity around zero).".

事實上,這正是這類問題的選項似乎旨在對付的。它可以看起來有點愚蠢,沿着你的對數刻度圖的底部有這個奇怪的線性區域,但你可以使它非常小。

+0

完美!它工作正常,如果我只是使用symlog選項,但出於某種原因,如果我試圖指定一個參數linthresy我得到一個TypeError:糟糕的操作數類型爲一元 - '元組' – user545424 2012-04-19 16:20:01

0

你總是可以從你目前的數字拉出座標appened一個額外的點到圖形的底部:

import numpy as np 
import pylab as plt 

# Create some sample data like yours 
X = np.linspace(0,3,100) 
Y = np.exp(-X) 

def semilogy_to_bottom(X,Y): 
    # Plot once to move axes and remove plot 
    P, = plt.semilogy(X,Y) 
    plt.gca().lines.remove(P) 

    # Find the bottom of the graph 
    y_min = plt.gca().get_ylim()[0] 

    # Add a new point 
    X2 = np.concatenate((X,[X[-1]])) 
    Y2 = np.concatenate((Y,[y_min])) 
    plt.semilogy(X2,Y2) 

semilogy_to_bottom(X,Y) 
plt.xlim(0,5) 
plt.show() 

enter image description here