简体   繁体   English

如何更改此代码以输出颜色取决于高度的直方图,而不是我选择的默认“酷”值

[英]How can I change this code to output an histogram with colors depending on height rather than the default 'cool' I chose

This code generates a probability distribution psi_0_x_squared. 此代码生成概率分布psi_0_x_squared。 Then performs a markov chain simulation according to this probability. 然后根据该概率执行马尔可夫链模拟。 This probability psi_0_x_squared is actually the probability to be at position x for energy level n=0. 该概率psi_0_x_squared实际上是能量水平n = 0处在位置x处的概率。 After moving x 1000 times according to this probability I want to generate a histogram of position x. 根据此概率移动x 1000次后,我想生成位置x的直方图。 (The position frequency) (位置频率)

''' Markov-chain Monte Carlo algorithm for a particle in a Gaussian potential,
using the Metropolis algorithm. '''
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

for k in range(1000):    #for this number of trials
    x_new = x + random.uniform(-delta, delta) #I displace it a distance delta


    if random.uniform(0.0, 1.0) < probability(x_new)/probability(x):
        x = x_new 
    data_x.append(x)

#histogram
cm = plt.cm.get_cmap('cool') 
n, bins, patches= plt.hist(data_x, bins=100, normed=True, color='k')
bin_centers = 0.5 * (bins[:-1] + bins[1:])
col = bin_centers - min(bin_centers)
col /= max(col)
for c, p in zip(col, patches):
    plt.setp(p, 'facecolor', cm(c))

plt.show()

The variable n contains the height of your bars. 变量n包含钢筋的高度。 Therefore this should do the trick: 因此,这应该可以解决问题:

for height, p in zip(n, patches):
    plt.setp(p, 'facecolor', cm(height))

like so: 像这样:

a = np.random.normal(size=(1000,))

cm = plt.cm.get_cmap('cool')
n, bins, patches= plt.hist(a, bins=100, normed=True, color='k')
for c, p in zip(n, patches):
    plt.setp(p, 'facecolor', cm(c))

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改此Python代码为多进程而不是多线程? - How can I change this Python code to be multiprocess rather than multi threaded? 我如何更改此 reducer 代码以查找最长的单词(和长度)而不是查找单词的频率? - How can i change this reducer code to find the longest words (and the length) rather than it finding the frequency of the words? 我在我的减速器输出中得到一个列表列表,而不是一个成对的值,我不确定在我的代码中要更改什么 - I'm getting a list of lists in my reducer output rather than a paired value and I am unsure of what to change in my code 如何根据 Y 轴的值更改图表数据点的 colors? (Python:Matplotlib) - How can I change the colors of a chart's data points depending on the value of the Y-axis? (Python: Matplotlib) 我可以将鼻子覆盖范围输出限制到目录(而不是包裹)吗? - Can I restrict nose coverage output to directory (rather than package)? 如何获得GTK中的默认颜色? - How can I get the default colors in GTK? 如何强制菜单位于 window 而不是 OSX 默认值 Tkinter - How can I force the Menu to be in the window rather than the OSX default Tkinter 我如何 plot 特定属性而不是时间序列中所有属性的默认值 - How can I plot specific attributes rather than default of all attributes in Time Series PYTHON:我应该如何更改我的代码以按天而不是按月重新组织文件? - PYTHON: How should I change my code to reorganize files by day rather than by month? 如何跳转到 python 中 pdb 中的代码而不是库代码? - How can I jump into my code in pdb in python rather than library code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM