如何在python中的条形图上添加标记? [英] How can I add markers on a bar graph in python?

查看:116
本文介绍了如何在python中的条形图上添加标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个水平条形图,现在我需要在条形上添加标记.我怎么能这样做?

I have made a horizontal bar graph, now I need to add markers on the bars. How can I do so?

我到目前为止的代码如下所示:

The code I have so far is shown below:

def plot_comparison(): 
lengths = [11380, 44547, 166616, 184373, 193068, 258004, 369582, 462795, 503099, 581158, 660724, 671812, 918449]

y_pos = np.arange(len(length))
error = np.random.rand(len(length))

plt.barh(y_pos, length, xerr=error, align='center', alpha=0.4)
plt.yticks(y_pos, length)
plt.xlabel('Lengths')
plt.title('Comparison of different cuts')
plt.show()

推荐答案

你可以简单地添加一个 plot 命令,根据 lengths 绘制 y_pos代码>.确保指定制造商并将线型设置为 ""(或 "none"),否则标记将通过直线连接.
以下代码可能正是您所追求的.

You can simply add a plot command, plotting the y_pos against the lengths. Make sure to specify a maker and set linestyle to "" (or "none") otherwise the markers will be connected by straight lines.
The following code may be what you're after.

import matplotlib.pyplot as plt
import numpy as np

lengths = [11380, 44547, 166616, 184373, 193068, 258004, 369582, 462795, 503099, 581158, 660724, 671812, 918449]

y_pos = np.arange(len(lengths))
error = np.array(lengths)*0.08

plt.barh(y_pos, lengths, xerr=error, align='center', alpha=0.4)
plt.plot(lengths, y_pos, marker="D", linestyle="", alpha=0.8, color="r")
plt.yticks(y_pos, lengths)
plt.xlabel('Lengths')
plt.title('Comparison of different cuts')
plt.show()

这篇关于如何在python中的条形图上添加标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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