python - matplotlib scatterplot(horizo​​ntal stem plot)中从x=0到数据点绘制水平线

标签 python matplotlib plot jupyter-notebook

考虑以下情节:

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

最佳答案

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

documentation hlines() 在 Matplotlib 网站上。

Plot with horizontal stem bars

关于python - matplotlib scatterplot(horizo​​ntal stem plot)中从x=0到数据点绘制水平线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14957567/

相关文章:

python - 如何保持字典的结构

python - 如何在 Debian Linux 上为 python 2.5 安装 SSL?

python - 如何更改matplotlib中双头注释的头大小?

python - 使用 Matplotlib 绘制 3D 绘图

matlab - Matlab 绘图的自定义标记

python - 编码base 64字符串

python - 跳过 NaN 值以获得距离

python - 使用 matplotlib轮廓/轮廓处理循环数据

r - 如何使用 dev.new() 在 knitr 小插图中显示图?

Swift - 图表框架 - 如何隐藏/显示线图?