栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python-在matplotlib条形图上添加值标签

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python-在matplotlib条形图上添加值标签

首先

freq_series.plot
返回一个轴而不是一个数字,以便使我的回答更加清楚。我已经更改了给定的代码以使其引用,
ax
而不是
fig
与其他代码示例更加一致。

你可以从该

ax.patches
成员处获得该图中所产生的钢筋的列表。然后,你可以使用此matplotlib库示例中演示的技术来使用ax.text方法添加标签。

import numpy as npimport pandas as pdimport matplotlib.pyplot as plt# Bring some raw data.frequencies = [6, 16, 75, 160, 244, 260, 145, 73, 16, 4, 1]# In my original pre I create a series and run on that, # so for consistency I create a series from the list.freq_series = pd.Series.from_array(frequencies)x_labels = [108300.0, 110540.0, 112780.0, 115020.0, 117260.0, 119500.0, 121740.0, 123980.0, 126220.0, 128460.0, 130700.0]# Plot the figure.plt.figure(figsize=(12, 8))ax = freq_series.plot(kind='bar')ax.set_title('Amount Frequency')ax.set_xlabel('Amount ($)')ax.set_ylabel('Frequency')ax.set_xticklabels(x_labels)rects = ax.patches# Make some labels.labels = ["label%d" % i for i in xrange(len(rects))]for rect, label in zip(rects, labels):    height = rect.get_height()    ax.text(rect.get_x() + rect.get_width() / 2, height + 5, label, ha='center', va='bottom')


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/614607.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号