2

考虑以下情节:

在此处输入图像描述

由这个函数产生:

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完成吗?需要添加什么代码?

4

2 回答 2

4

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

文档hlines()Matplotlib 网站上。

带有水平茎杆的绘图

于 2013-02-19T13:27:14.463 回答
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()], '--')

结果如下:

带有水平茎杆的绘图

但是请注意,正如 David Zwicker 指出的那样,当 x 在对数刻度上时,从 x = 0 绘制条形没有意义,因为 x = 0 在 x 轴的左侧无限远。

于 2013-02-19T12:45:38.867 回答