如何将"LinearSegmentedColormap"更改为不同的颜色分布? [英] How to change a 'LinearSegmentedColormap' to a different distribution of color?

查看:36
本文介绍了如何将"LinearSegmentedColormap"更改为不同的颜色分布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个偏向"较低值的颜色图,即从较深的颜色变为浅色需要更长的时间.目前我正在使用它作为颜色图:

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

我正在围绕圆柱体绘制此图形以查看效果(请参阅文章末尾的圆柱体代码),这是在运行代码时发生的情况:

如您所见,这是非常线性"的.颜色大约在圆柱体的一半处开始变化.有什么方法可以增加颜色开始快速变化的阈值?IE.我只希望非常高的数字具有最亮的黄色.谢谢.

from matplotlib import cm从 matplotlib 导入 pyplot 作为 plt从mpl_toolkits.mplot3d导入Axes3D从 scipy.linalg 导入规范从 mpl_toolkits.mplot3d.art3d 导入 Poly3DCollection将numpy导入为np导入数学将 mpl_toolkits.mplot3d.art3d 导入为 art3d导入matplotlib.colors作为clr无花果= plt.figure()ax = fig.add_subplot(111, 投影='3d')原点 = [0,0,0]#半径 = Rp0 = np.array(原点)p1 = np.array([8, 8, 8])origin = np.array(origin)R = 1#轴方向向量v = p1 - p0#找到向量的大小mag =范数(v)#轴方向的单位向量v = v/mag#使某些向量与v方向不同not_v = np.array([1, 0, 0])如果(v == not_v).all():not_v = np.array([0,1,0])#使向量垂直于vn1 = np.cross(v, not_v)#标准化n1n1/= 范数(n1)#make垂直于v和n1的单位向量n2 = np.cross(v,n1)#surface 范围从 0 到轴长度和 0 到 2*pit = np.linspace(0,mag,600)theta = np.linspace(0,2 * np.pi,100)#使用meshgrid制作二维数组t,theta = np.meshgrid(t,theta)#生成曲面的坐标对于[]中的i,X,Y,Z = [p0 [i] + v [i] * t + R * np.sin(theta)* n1 [i] + R * np.cos(theta)* n2 [i]0, 1, 2]]#这是彩色地图所在的位置cmap = clr.LinearSegmentedColormap.from_list('自定义蓝色', ['#ffff00','#002266'], N=256)col1 = cmap(np.linspace(0,1,600))#沿t轴的线性渐变col1 = np.repeat(col1[np.newaxis,:, :], 100, axis=0) # 在 theta 轴上展开ax.plot_surface(X,Y,Z,facecolors = col1,shade = True,edgecolors ="None",alpha = 0.9,线宽= 0)ax.view_init(15,-40)plt.show()

解决方案

使用 LinearSegmentedColormap.from_list 制作颜色图时,您可以提供格式(值,颜色)(相反)的元组列表以简化为颜色列表),其中值对应于颜色的相对位置.值的范围必须从 01,因此您必须提供中间颜色.在你的情况下,我可能会尝试这个,

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

并调整颜色/值直到满意为止.归功于

I am trying to make a color map that 'favors' lower values, i.e. it takes longer to get out of the darker color to get to the light color. At the moment I am using this as the colormap:

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

I am plotting this around a cylinder to see the effect (see code for cylinder at the end of the post), this is what happens when you run the code:

As you can see this is very 'linear'. The color starts changing about halfway along the cylinder. Is there a way to increase the threshold for when the colors start to change rapidly? I.e. I want only very high numbers to have the brightest level of yellow. Thanks.

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

解决方案

When making colormaps with LinearSegmentedColormap.from_list, you can supply a list of tuples of the form (value, color) (as opposed to simply a list of colors) where the values correspond to the relative positions of colors. The values must range from 0 to 1 so you will have to supply an intermediate color. In your case I might try this,

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

and tweak color/value until satisfied. Credit goes to https://stackoverflow.com/a/25000108/5285918

这篇关于如何将"LinearSegmentedColormap"更改为不同的颜色分布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆