显示在条形图中绘制的y轴值水平线 [英] Display y axis value horizontal line drawn In bar chart

查看:115
本文介绍了显示在条形图中绘制的y轴值水平线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用(matplotlib.pyplot as plt)matplotlib绘制条形图.在该条形图上,我使用axhline()函数以灰色绘制了一条水平线.我希望从该水平线开始的点(y轴上的值= 42000)也应该显示值,即42000.该怎么办?

I am using (matplotlib.pyplot as plt) matplotlib to draw a bar chart. On that bar chart I have drawn a horizontal line using axhline() function in grey colour . I want that the point (value on y axis = 42000) from where that horizontal line starts should also display the value i.e 42000 . What to do?

这是我当前的图像:

在下图上,看到"39541.52"点吗?我想在图像上显示完全一样的图像,并且我的分值是"42000"

On the image below, see the '39541.52' point? I want to display exactly like that on my image and my point value is '42000'

推荐答案

可以创建标签,例如使用ax.text().放置标签的一个不错的技巧是使用一种转换,该转换允许将轴坐标用于x位置,将数据坐标用于y位置.

A label can be created e.g. using ax.text(). To position the label a nice trick is to use a transform that allows to use axes coordinates for the x position and data coordinates for the y position.

ax.text(1.02, 4.2e4, "42000", .. , transform=ax.get_yaxis_transform())

完整代码:

import matplotlib.pyplot as plt
import matplotlib.colors
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
x = [0,1,2,3]
y = np.array([34,40,38,50])*1e3
norm = matplotlib.colors.Normalize(30e3, 60e3)
ax.bar(x,y, color=plt.cm.plasma_r(norm(y)) )
ax.axhline(4.2e4, color="gray")
ax.text(1.02, 4.2e4, "42000", va='center', ha="left", bbox=dict(facecolor="w",alpha=0.5),
        transform=ax.get_yaxis_transform())
plt.show()

这篇关于显示在条形图中绘制的y轴值水平线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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