python bar函数默认颜色_python – 更改所选matplotlib直方图bin bar的颜色,给定它的值...

使用rectangle.get_x()可以很容易地得到条形的x坐标,但问题是条形图没有完全按特定值绘制,所以我必须选择最接近的条形图.这是我的解决方案:

import pandas as pd

import matplotlib.pyplot as plt

import numpy as np

s = pd.Series(np.random.normal(0, 100, 10000))

p = s.plot(kind='hist', bins=50, color='orange')

bar_value_to_label = 100

min_distance = float("inf") # initialize min_distance with infinity

index_of_bar_to_label = 0

for i, rectangle in enumerate(p.patches): # iterate over every bar

tmp = abs( # tmp = distance from middle of the bar to bar_value_to_label

(rectangle.get_x() +

(rectangle.get_width() * (1 / 2))) - bar_value_to_label)

if tmp < min_distance: # we are searching for the bar with x cordinate

# closest to bar_value_to_label

min_distance = tmp

index_of_bar_to_label = i

p.patches[index_of_bar_to_label].set_color('b')

plt.show()

收益:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值