在Matplotlib散点图中绘制从x = 0到数据点的水平线(水平茎图) [英] Draw horizontal lines from x=0 to data points in matplotlib scatterplot (horizontal stem plot)

查看:247
本文介绍了在Matplotlib散点图中绘制从x = 0到数据点的水平线(水平茎图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下情节:

此函数产生的:

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的值的正或负差.最好将其可视化,例如

What matters here is the positive or negative difference of the values to x = 0. It would be nice to visualize this more clearly, e.g.

  • 强调x = 0轴
  • 从x = 0到情节标记画一条线

这可以用matplotlib完成吗?需要添加什么代码?

Can this be done with matplotlib? What code would need to be added?

推荐答案

正如罗格·卡西斯(Rutger Kassies)所指出的那样,实际上有一些茎"功能可以使我的其他答案中的手动"方法自动化.水平茎线的功能为hlines()(垂直茎杆的功能为vlines()):

As pointed out by Rutger Kassies, there are actually some "stem" functions that automate the "manual" method from my other answer. The function for horizontal stem lines is hlines() (vlines() is for vertical stem bars):

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

Matplotlib网站上的hlines()文档

The documentation for hlines() is on the Matplotlib website.

这篇关于在Matplotlib散点图中绘制从x = 0到数据点的水平线(水平茎图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆