画水平线从x = 0到在matplotlib散点图的数据点(水平干曲线)

问题描述:

考虑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

正如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

(见我对方的回答,对于一个更快的解决方案。)

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轴的左边无限远。