【问题标题】:How can I change the color of an histogram label icon?如何更改直方图标签图标的颜色?
【发布时间】:2018-02-17 21:15:22
【问题描述】:

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

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()

【问题讨论】:

    标签: python matplotlib plot histogram bar-chart


    【解决方案1】:

    一种解决方案是将生成legend 放在更新histfacecolor 之前。另外,我注意到一个小问题,为什么在生成legend 之前保存图形?这是固定的代码和结果:

    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')
    
    
    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.")
    # first legend, then updating the facecolor
    plt.legend()
    for height, p in zip(n, patches):
        plt.setp(p, 'facecolor', cm(height))
    # finally save the figure
    plt.savefig('ground_probability_x.png')
    plt.show()
    

    希望对您有所帮助。谢谢。

    【讨论】:

    • 是的,您对保存部分是正确的。并感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 2017-07-09
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    相关资源
    最近更新 更多