本文介绍了在 matplotlib 中设置 Colorbar 颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

 将matplotlib.pyplot导入为plt将numpy导入为np从 scipy 导入集成,特殊导入pylab定义 f(v,r):"""初始函数"""阿尔法 = 0.2chi = 1/2*special.erf((r+1.2)/0.3)-1/2*special.erf((r-1.2)/0.3)返回4/(np.sqrt(2 * np.pi * alpha))* chi * np.exp(-v ** 2/(2 * alpha))N_xi = 100v =(np.arange(N_xi)* 4/(N_xi-1)-2)r = (np.arange(N_xi)*4/(N_xi - 1)-2)z = f(v.reshape(-1,1),r.reshape(1,-1))pylab.pcolor(v,r,z)pylab.colorbar()pylab.show()

我得到了图:但我想更改其颜色,以得到这样的图形如何设置颜色?

解决方案

您需要指定 jet

I have the following code:

import matplotlib.pyplot as plt
import numpy as np
from scipy import integrate, special
import pylab
def f(v,r):
    """ Initial function"""
    alpha = 0.2
    chi = 1/2*special.erf((r+1.2)/0.3)-1/2*special.erf((r-1.2)/0.3)
    return 4/(np.sqrt(2*np.pi*alpha))*chi*np.exp(-v**2/(2*alpha))


N_xi = 100

v = (np.arange(N_xi)*4/(N_xi - 1)-2)
r = (np.arange(N_xi)*4/(N_xi - 1)-2)
z = f(v.reshape(-1, 1), r.reshape(1, -1))

pylab.pcolor(v, r, z)

pylab.colorbar()

pylab.show()

And I got the figure:But I want to change the color of it, to get the figure like thisHow can I set the color?

解决方案

You need to specify the jet colormap when creating the pcolor object

pylab.pcolor(v, r, z, cmap='jet')

That being said, the jet colormap isn't ideal for many situations so take care to choose an appropriate colormap.

这篇关于在 matplotlib 中设置 Colorbar 颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 05:24