密密麻麻-不同颜色的表面 [英] Plotly - different color surfaces

查看:69
本文介绍了密密麻麻-不同颜色的表面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Plotly for Python绘制多个不同颜色的表面.

I'm trying to plot several surfaces, each of a different color, in Plotly for Python.

具体地说,表面显示了在相空间中不同点处采取动作的预测奖励函数.由于我在每个点上都有几种可能的动作,因此每个动作都是不同的表面.我想对每个表面进行唯一着色,但与x,y或z坐标无关.

Specifically, a surface shows the predicted reward function for taking an action at different points in phase space. Since I have several possible actions at each point, each is a different surface. I'd like to color each surface uniquely, but independent of the x,y, or z coordinate.

我尝试遵循 R 中的答案,但是我可以不知道我做错了什么.我总是得到相同的蓝色.由于我在代码的其他部分中使用了PyPlot,因此我从默认的matplotlib表中选择颜色.

I've tried to follow answer in R, but I can't figure out what I've done wrong. I always get the same blue color. Since I'm using PyPlot in other parts of my code, I'm choosing colors from the default matplotlib tableau.

这是玩具数据的基本示例.

Here's a basic example with toy data.

import matplotlib.pyplot as plt
import numpy as np
import plotly.graph_objs as go
import plotly.offline as off

off.init_notebook_mode()

make_int = np.vectorize(int)
cmap = plt.get_cmap("tab10")

saddle = np.array([[x**2-y**2 for x in np.arange(-10,11)] for y in np.arange(-10,11)])
paraboloid = np.array([[x**2 + y**2-100 for x in np.arange(-10,11)] for y in np.arange(-10,11)])

mycolors_a = make_int(256*np.array(cmap(1)[0:3])).reshape((1, 1,-1)).repeat(21, axis = 0).repeat(21, axis =1)
mycolors_b = make_int(256*np.array(cmap(2)[0:3])).reshape((1, 1,-1)).repeat(21, axis = 0).repeat(21, axis =1)
trace_a = go.Surface(z = saddle, surfacecolor = mycolors_a, opacity = .7, showscale = False, name = "Trace A")
trace_b = go.Surface(z = paraboloid, surfacecolor = mycolors_b, opacity = .7, showscale = False, name = "Trace B")

data = [trace_a, trace_b]
off.iplot(data)

产生以下内容:

我应该看到一个蓝色的鞍和一个橙色的抛物面,但是我没有.请注意,即使将参数更改为cmap,我也始终获得相同的蓝色.感谢您的帮助!

I should see a blue saddle and an orange paraboloid, but I don't. Note that even if I change the argument to cmap, I always get the same blue color. Thanks for your help!

推荐答案

文档在这里有点神秘.

surfacecolor

surfacecolor

(列表,numpy数组或Pandas系列的数字,字符串或日期时间.)

(list, numpy array, or Pandas series of numbers, strings, or datetimes.)

设置表面颜色值,用于设置独立于z的色标.

Sets the surface color values, used for setting a color scale independent of z.

我从来没有设法放入字符串列表,例如'rgb(0.3,0.5,0)'或RGB元组之类的颜色值.

I never managed to put a list of strings, i.e. color values like 'rgb(0.3, 0.5, 0)', or RGB tuples in it.

但是您可以使用所需的颜色定义自己的色阶.

But you can define your own color scale with the needed colors.

colorscale = [[0, 'rgb' + str(cmap(1)[0:3])], 
              [1, 'rgb' + str(cmap(2)[0:3])]]

,然后提供一个数字数组,其尺寸与绘制的值相同.

and then provide a numeric array with the same dimensions as your plotted values.

colors_saddle = np.zeros(shape=saddle.shape)    

所有值都设置为0,因此将映射到colorscale中的第一种颜色.下一种颜色也一样.

All values are set to 0 and will therefore map to the first color in your colorscale. The same for the next color.

此外,您需要手动设置cmaxcmin.

In addition you need to set cmax and cmin manually.

完整代码

import numpy as np
import matplotlib.pyplot as plt
import plotly.graph_objs as go
import plotly.offline as off


off.init_notebook_mode()

make_int = np.vectorize(int)
cmap = plt.get_cmap("tab10")

saddle = np.array([[x**2-y**2 for x in np.arange(-10,11)] for y in np.arange(-10,11)])
paraboloid = np.array([[x**2 + y**2-100 for x in np.arange(-10,11)] for y in np.arange(-10,11)])

colors_saddle = np.zeros(shape=saddle.shape)    
colors_paraboloid = np.ones(shape=paraboloid.shape)    

colorscale = [[0, 'rgb' + str(cmap(1)[0:3])], 
              [1, 'rgb' + str(cmap(2)[0:3])]]

trace_a = go.Surface(z=saddle, 
                     surfacecolor=colors_saddle, 
                     opacity=.7, 
                     name="Trace A",
                     cmin=0,
                     cmax=1,
                     colorscale=colorscale)
trace_b = go.Surface(z=paraboloid, 
                     surfacecolor=colors_paraboloid, 
                     opacity=.7, 
                     name="Trace B", 
                     cmin=0,
                     cmax=1,
                     showscale=False,
                     colorscale=colorscale)

data = [trace_a, trace_b]
off.iplot(data)

这篇关于密密麻麻-不同颜色的表面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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