我被困在一些感觉应该相对容易的事情上.我下面的代码是基于我正在研究的更大项目的示例.我没有理由发布所有细节,所以请接受我带来的数据结构.
基本上,我正在创建一个条形图,我只是想弄清楚如何在条形图上添加值标签(在条形图的中心,或者在它上面).一直在寻找网络上的样本,但没有成功实现我自己的代码.我相信解决方案要么是'text',要么是'annotate',但是我:a)不知道使用哪一个(一般来说,还没弄清楚何时使用哪个).b)无法看到要么呈现价值标签.非常感谢您的帮助,我的代码如下.提前致谢!
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
pd.set_option('display.mpl_style', 'default')
%matplotlib inline
# Bring some raw data.
frequencies = [6, 16, 75, 160, 244, 260, 145, 73, 16, 4, 1]
# In my original code 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. …Run Code Online (Sandbox Code Playgroud) 我目前正在使用Matplotlib来创建直方图:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as pyplot
...
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1,)
n, bins, patches = ax.hist(measurements, bins=50, range=(graph_minimum, graph_maximum), histtype='bar')
#ax.set_xticklabels([n], rotation='vertical')
for patch in patches:
patch.set_facecolor('r')
pyplot.title('Spam and Ham')
pyplot.xlabel('Time (in seconds)')
pyplot.ylabel('Bits of Ham')
pyplot.savefig(output_filename)
Run Code Online (Sandbox Code Playgroud)
我想让x轴标签更有意义.
首先,这里的x轴刻度似乎限于五个刻度.无论我做什么,我似乎无法改变这一点 - 即使我添加更多xticklabels,它只使用前五个.我不确定Matplotlib如何计算这个,但我认为它是从范围/数据中自动计算的?
有没有什么办法可以提高x-tick标签的分辨率 - 甚至可以提高每个条形码/ bin 的分辨率?
(理想情况下,我也希望以微秒/毫秒重新格式化秒数,但这是另一天的问题).
其次,我想要标记每个单独的条形图 - 包含该条形图中的实际数字,以及所有条形图总数的百分比.
最终输出可能如下所示:

Matplotlib有可能吗?
干杯,维克多
有没有办法在 distplot 上绘制百分比而不是计数?
ax = sns.FacetGrid(telcom, hue='Churn', palette=["teal", "crimson"], size=5, aspect=1)
ax = ax.map(sns.distplot, "tenure", hist=True, kde=False)
ax.fig.suptitle('Tenure distribution in customer churn', y=1, fontsize=16, fontweight='bold');
plt.legend();
Run Code Online (Sandbox Code Playgroud)
python ×3
matplotlib ×2
pandas ×2
displot ×1
graphing ×1
histogram ×1
histplot ×1
python-2.7 ×1
seaborn ×1