如何更改直方图标签图标的颜色?

时间:2017-09-08 21:58:31

标签: python matplotlib plot histogram bar-chart

这是代码。但是,直方图有一个黑色图标作为标签。如何将其更改为红色或渐变的颜色' hot'。我知道我将条形颜色更改为渐变效果' hot'但是我不想要直方图的黑色标签,因为我想将它与分析图区分开来。

import math, matplotlib.pyplot as plt, random 

def probability(x):

    #wavefunction n=0 evaluated at position x
    psi_0_x=math.exp(-x ** 2 / 2.0) / math.pi ** 0.25

    #probability n=0 to be at position x
    psi_0_x_squared= psi_0_x**2

    return psi_0_x_squared

data_x=[0]
x = 0.0        #starts at position 0
delta = 0.5    #stepsize
trial_steps=1000000

for t in range(trial_steps):

    #displace x by delta
    x_new = x + random.uniform(-delta, delta) 

    #selecciono un numero entre 0 y 1 (incluye acceptance y rejection probability). Metropolis!
    #probabilidad de estar en nuevo sitio/probabilidad de quedarme en el sitio anterior 
    if random.uniform(0.0, 1.0) < probability(x_new)/probability(x):

        #me muevo si la condicion es cierta (está en el accepted range)
        x = x_new 
    data_x.append(x)

#histogram
cm = plt.cm.get_cmap('hot') 
n, bins, patches= plt.hist(data_x, bins=100, normed=True, color='r',label='Histogram')
for height, p in zip(n, patches):
    plt.setp(p, 'facecolor', cm(height))

plt.xlabel('x')
plt.ylabel('$Probability =|\psi_0(x)|^2$')

#general analytical formula
x_grid = [a / 100.0 for a in range(-300,301)]
Prob = [probability(position) for position in x_grid]
plt.plot(x_grid, Prob, linewidth=1.5, color='k', label='Analytical')

plt.title("Position's probability density $|\psi_0(x)|^2$ for a harmonic oscillator.")
plt.savefig('ground_probability_x.png')
plt.legend()
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:1)

一种解决方案是在更新import React from 'react'; import $ from 'jquery'; class MobileMenu extends React.Component { constructor (props) { super(props); this.clickHandler1 = this.clickHandler1.bind(this); this.clickHandler2 = this.clickHandler2.bind(this); this.clickHandler3 = this.clickHandler3.bind(this); this.state = { current_item: 'nav_fave' previous_item: null }; } clickHandler1 () { this.toggleMenu(); } clickHandler2 () { // MenuPage.flip('fave'); this.toggleMenu(); this.toggleMarker($A.el('#nav_fave')); } clickHandler3 () { // MenuPage.flip('splash'); this.toggleMenu(); this.toggleMarker($A.el('#nav_splash')); } toggleMarker (current_item) { this.setState({ current_item: current_item }); if ((this.previous_item !== undefined) && (this.previous_item !== current_item)) { this.previous_item.style.borderBottom = ''; } if (this.previous_item !== current_item) { current_item.style.borderBottom = '3px solid #31baed'; } this.previous_item = current_item; } toggleMenu () { if (window.getComputedStyle($A.el('#top_menu_list'), null).display === 'block') { $('#icon_bars').toggleClass('active'); $('#top_menu').slideToggle(); } } // ... snip } export default MobileMenu 的{​​{1}}之前生成legend。另外,我注意到一个小问题,为什么你甚至在生成facecolor之前就保存了这个数字?这是固定的代码和结果:

hist

enter image description here

希望它会有所帮助。感谢。