python - 如何将 'LinearSegmentedColormap' 更改为不同的颜色分布?

标签 python matplotlib

我正在尝试制作一个“有利于”较低值的颜色图,即从较深的颜色到浅色需要更长的时间。目前我正在使用它作为颜色图:

cmap = clr.LinearSegmentedColormap.from_list('custom blue', ['#ffff00','#002266'], N=256)

我正在围绕一个圆柱体绘制它以查看效果(参见帖子末尾的圆柱体代码),这是运行代码时发生的情况:

enter image description here

如您所见,这是非常“线性”的。颜色开始在圆柱体的一半左右发生变化。有没有办法增加颜色开始快速变化的阈值? IE。我只希望非常高的数字具有最亮的黄色。谢谢。

from matplotlib import cm
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.linalg import norm
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
import math
import mpl_toolkits.mplot3d.art3d as art3d
import matplotlib.colors as clr


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')



origin = [0,0,0]
#radius = R
p0 = np.array(origin)
p1 = np.array([8, 8, 8])
origin = np.array(origin)
R = 1

#vector in direction of axis
v = p1 - p0
#find magnitude of vector
mag = norm(v)
#unit vector in direction of axis
v = v / mag
#make some vector not in the same direction as v
not_v = np.array([1, 0, 0])
if (v == not_v).all():
not_v = np.array([0, 1, 0])
#make vector perpendicular to v
n1 = np.cross(v, not_v)
#normalize n1
n1 /= norm(n1)
#make unit vector perpendicular to v and n1
n2 = np.cross(v, n1)
#surface ranges over t from 0 to length of axis and 0 to 2*pi
t = np.linspace(0, mag, 600)
theta = np.linspace(0, 2 * np.pi, 100)
#use meshgrid to make 2d arrays
t, theta = np.meshgrid(t, theta)
#generate coordinates for surface
X, Y, Z = [p0[i] + v[i] * t + R * np.sin(theta) * n1[i] + R * np.cos(theta) *     n2[i] for i in [0, 1, 2]]

#THIS IS WHERE THE COLOR MAP IS

cmap = clr.LinearSegmentedColormap.from_list('custom blue',     ['#ffff00','#002266'], N=256)
col1 = cmap(np.linspace(0,1,600)) # linear gradient along the t-axis
col1 = np.repeat(col1[np.newaxis,:, :], 100, axis=0) # expand over the theta-            axis

ax.plot_surface(X, Y,Z, facecolors = col1, shade = True,edgecolors = "None",        alpha = 0.9, linewidth = 0)
ax.view_init(15,-40)

plt.show()

最佳答案

使用 LinearSegmentedColormap.from_list 制作颜色图时,您可以提供形式为 (value, color) 的元组列表(而不是简单的颜色列表),其中值对应于相对颜色的位置。值的范围必须从 01,因此您必须提供中间颜色。在你的情况下,我可以试试这个,

cmap = clr.LinearSegmentedColormap.from_list('custom blue', 
                                             [(0,    '#ffff00'),
                                              (0.25, '#002266'),
                                              (1,    '#002266')], N=256)

并调整颜色/值直到满意为止。归功于https://stackoverflow.com/a/25000108/5285918

enter image description here

关于python - 如何将 'LinearSegmentedColormap' 更改为不同的颜色分布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38147997/

相关文章:

python - 根据索引列合并两个数据框

python - Pycharm 缩进问题

python - 绘制 Pandas 时间序列数据框的线性回归线的置信区间

python - 为什么参数不能在 pyplot 中以 x 和 y 的形式显式传递?

python - 为什么matplotlib默认不保存整图?

Python:初始化空嵌套列表时的奇怪列表行为

python - Jupyter 笔记本无法从 Centos 7 打开

python - Gremlin 绑定(bind)方法支持

python - Matplotlib sharex 对具有不同 x 值的数据?

python - CDF、matplotlib - 绘图、python 的颜色不足