2013-02-19 254 views
2

考慮follwing情節:畫水平線從x = 0到在matplotlib散點圖的數據點(水平幹曲線)

enter image description here

由該函數產生:

def timeDiffPlot(dataA, dataB, saveto=None, leg=None): 
    labels = list(dataA["graph"]) 
    figure(figsize=screenMedium) 
    ax = gca() 
    ax.grid(True) 
    xi = range(len(labels)) 
    rtsA = dataA["running"]/1000.0 # running time in seconds 
    rtsB = dataB["running"]/1000.0 # running time in seconds 
    rtsDiff = rtsB - rtsA 
    ax.scatter(rtsDiff, xi, color='r', marker='^') 
    ax.scatter 
    ax.set_yticks(range(len(labels))) 
    ax.set_yticklabels(labels) 
    ax.set_xscale('log') 
    plt.xlim(timeLimits) 
    if leg: 
     legend(leg) 
    plt.draw() 
    if saveto: 
     plt.savefig(saveto, transparent=True, bbox_inches="tight") 

這裏的問題是與x = 0的值的正面或負面差異。能夠更清楚地顯示這一點是很好的,例如

  • 強調x = 0的軸
  • 繪製從x = 0的線的情節標記

可以這樣與matplotlib做什麼?需要添加哪些代碼?

+0

要繪製一個「行」從x = 0的點,你應該簡單地嘗試進行柱狀圖,對現有的替代或疊加。 – 2013-02-19 12:35:02

+3

你有一個對數圖,即點x = 0不能顯示。 – 2013-02-19 12:37:19

+0

你可以使用ax.vlines()或ax.axvline(),但實際上它們不會在日誌上顯示x = 0。 – 2013-02-19 12:58:31

回答

2

正如Rutger Kassies指出的那樣,實際上有一些「幹」功能可以從我的其他答案中自動化「手動」方法。對於水平柱線功能是hlines()vlines()是豎直柱條):

import numpy 
from matplotlib import pyplot 

x_arr = numpy.random.random(10)-0.5; y_arr = numpy.arange(10) 

pyplot.hlines(y_arr, 0, x_arr, color='red') # Stems 
pyplot.plot(x_arr, y_arr, 'D') # Stem ends 
pyplot.plot([0, 0], [y_arr.min(), y_arr.max()], '--') # Middle bar 

documentationhlines()是Matplotlib網站上。

Plot with horizontal stem bars

1

(見我對方的回答,對於一個更快的解決方案。)

Matplotlib提供垂直的 「幹」 吧:http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.stem。但是,我找不到stem()的水平等價物。

通過重複調用plot()(每個詞幹一個),仍然可以很容易地繪製水平幹線條。下面是一個例子

import numpy 
from matplotlib.pyplot import plot 

x_arr = numpy.random.random(10)-0.5; y_arr = numpy.arange(10) 

# Stems: 
for (x, y) in zip(x_arr, y_arr): 
    plot([0, x], [y, y], color='red') 
# Stem ends: 
plot(x_arr, y_arr, 'D') 
# Middle bar: 
plot([0, 0], [y_arr.min(), y_arr.max()], '--') 

結果如下:

Plot with horizontal stem bars

注意,但是,從x = 0繪製的酒吧沒有什麼意義,當x是對數尺度,大衛Zwicker指出,因爲x = 0在x軸的左邊無限遠。