簡體   English   中英

python在直方圖中標記新數據點

[英]python labelling new data points in a histogram

我目前正在使用此代碼繪制直方圖。

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 值。

除了這些行外,所有內容都保持不變:

...
# 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 值(彼此非常接近),這是預期的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM