【问题标题】:python labelling new data points in a histogrampython在直方图中标记新数据点
【发布时间】:2021-12-01 15:27:48
【问题描述】:

我目前正在使用此代码绘制直方图。

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FormatStrFormatter

data = np.random.randn(82)
fig, ax = plt.subplots()
counts, bins, patches = ax.hist(data, facecolor='yellow', edgecolor='gray')

# Set the ticks to be at the edges of the bins.
ax.set_xticks(bins)
# Set the xaxis's tick labels to be formatted with 1 decimal place...
ax.xaxis.set_major_formatter(FormatStrFormatter('%0.1f'))

# Change the colors of bars at the edges...
twentyfifth, seventyfifth = np.percentile(data, [25, 75])
for patch, rightside, leftside in zip(patches, bins[1:], bins[:-1]):
    if rightside < twentyfifth:
        patch.set_facecolor('green')
    elif leftside > seventyfifth:
        patch.set_facecolor('red')

# Label the raw counts and the percentages below the x-axis...
bin_centers = 0.5 * np.diff(bins) + bins[:-1]
for count, x in zip(counts, bin_centers):
    # Label the raw counts
    ax.annotate(str(count), xy=(x, 0), xycoords=('data', 'axes fraction'),
        xytext=(0, -18), textcoords='offset points', va='top', ha='center')

    # Label the percentages
    percent = '%0.0f%%' % (100 * float(count) / counts.sum())
    ax.annotate(percent, xy=(x, 0), xycoords=('data', 'axes fraction'),
        xytext=(0, -32), textcoords='offset points', va='top', ha='center')


# Give ourselves some more room at the bottom of the plot
plt.subplots_adjust(bottom=0.15)
plt.show()

我想在直方图上添加给定直方图 x 轴值的 x 标记(标有“橙色”、“苹果”、“菠萝”),如图所示:

我应该怎么做?

x 标记没有 y 值。

【问题讨论】:

    标签: python matplotlib label histogram white-labelling


    【解决方案1】:

    除了以下几行之外,所有内容都保持不变:

    ...
    # Change the colors of bars at the edges...
    left = []
    right = []
    twentyfifth, seventyfifth = np.percentile(data, [25, 75])
    for patch, rightside, leftside in zip(patches, bins[1:], bins[:-1]):
        if rightside < twentyfifth:
            patch.set_facecolor('green')
            left.append(leftside)
    
        elif leftside > seventyfifth:
            patch.set_facecolor('red')
            right.append(rightside)
    
    ax.text(left[int(len(left)/2)], 1, 'orange\n    x')
    ax.text(right[0], 1, 'pineapple\n       x')
    ax.text((left[int(len(left)/2)] + right[0]) / 2, 1, 'apple\n   x')
    
    # Label the raw counts and the percentages below the x-axis...
    bin_centers = 0.5 * np.diff(bins) + bins[:-1]
    ...
    

    输出:

    ---编辑---

    OP 添加了数据并要求进行编辑。

    问题中OP的代码保持原样,后面要添加以下行。

    data = {'product_name': ['laptop', 'printer', 'tablet', 'desk', 'chair'],'price': [2, 0.1, 2.4, 2.2, 1]}
    ax.scatter(data['price'], [1]*len(data['price']), zorder=2, marker='x', c='k')
    for i in range(len(data['price'])):
        ax.text(data['price'][i]-0.2, 1.5, f"{data['product_name'][i]}")
    

    输出:

    注释是重叠的,但这是预期的,因为 OP 给出的 x 值(彼此非常接近)。

    【讨论】:

    • 感谢您的回答!但这不是我正在寻找的东西。所以我有一个数据框data = {'product_name': ['laptop', 'printer', 'tablet', 'desk', 'chair'],'price': [1200, 150, 300, 450, 200]}“价格”列是我想在直方图顶部绘制的 x,而“产品名称”列是我希望每个点都被标记的标签。 @卡琳娜
    • 我不明白。如果您的x 值在1501200 的范围内,它应该在哪里?例如,您绘制xlim 基本上只是-2,22,6
    • data = {'product_name': ['laptop', 'printer', 'tablet', 'desk', 'chair'],'price': [2, 0.1, 2.4, 2.2, 1]} 为混乱道歉。我已经修改了dataframe,这种情况下代码会是什么样子。
    • 只是让您知道,这与您的问题中显示的完全不同。如果您在问题中包含此信息,将会更有成效。
    • 注释是重叠的,但由于数据的性质,我希望您能预料到这一点。另一种方法是动态更改注释标签的位置。但这不在这个问题范围内。
    猜你喜欢
    • 1970-01-01
    • 2021-07-22
    • 2017-05-12
    • 2013-12-06
    • 2020-06-07
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 2020-12-29
    相关资源
    最近更新 更多