Plotly - different color surfaces
I'm trying to plot several surfaces, each of a different color, in Plotly for Python.
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.
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)
Produces the following:
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!
python python-3.x jupyter-notebook plotly
add a comment |
I'm trying to plot several surfaces, each of a different color, in Plotly for Python.
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.
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)
Produces the following:
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!
python python-3.x jupyter-notebook plotly
add a comment |
I'm trying to plot several surfaces, each of a different color, in Plotly for Python.
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.
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)
Produces the following:
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!
python python-3.x jupyter-notebook plotly
I'm trying to plot several surfaces, each of a different color, in Plotly for Python.
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.
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)
Produces the following:
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!
python python-3.x jupyter-notebook plotly
python python-3.x jupyter-notebook plotly
edited Jan 2 at 16:13
Jake Stevens-Haas
asked Jan 1 at 2:01
Jake Stevens-HaasJake Stevens-Haas
3271210
3271210
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The documentation is a bit cryptic here.
surfacecolor
(list, numpy array, or Pandas series of numbers, strings, or datetimes.)
Sets the surface color values, used for setting a color scale independent of
z
.
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)
All values are set to 0
and will therefore map to the first color in your colorscale
. The same for the next color.
In addition you need to set cmax
and cmin
manually.
Complete code
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)
Thanks! That's a great example. I'm not sure this warrants a separate question, but do you know why Plotly doesn't display overlaps correctly with transparency? It seems to suffer from the same problems as matplot3d. That is, even at 99% opacity, whichever trace is added to data last appears in front. Only at 100% opacity do the overlaps show correctly
– Jake Stevens-Haas
Jan 2 at 16:19
@JakeStevens-Haas: No idea. I'd guess it would be best to open an issue on Github.
– Maximilian Peters
Jan 2 at 17:45
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992606%2fplotly-different-color-surfaces%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The documentation is a bit cryptic here.
surfacecolor
(list, numpy array, or Pandas series of numbers, strings, or datetimes.)
Sets the surface color values, used for setting a color scale independent of
z
.
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)
All values are set to 0
and will therefore map to the first color in your colorscale
. The same for the next color.
In addition you need to set cmax
and cmin
manually.
Complete code
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)
Thanks! That's a great example. I'm not sure this warrants a separate question, but do you know why Plotly doesn't display overlaps correctly with transparency? It seems to suffer from the same problems as matplot3d. That is, even at 99% opacity, whichever trace is added to data last appears in front. Only at 100% opacity do the overlaps show correctly
– Jake Stevens-Haas
Jan 2 at 16:19
@JakeStevens-Haas: No idea. I'd guess it would be best to open an issue on Github.
– Maximilian Peters
Jan 2 at 17:45
add a comment |
The documentation is a bit cryptic here.
surfacecolor
(list, numpy array, or Pandas series of numbers, strings, or datetimes.)
Sets the surface color values, used for setting a color scale independent of
z
.
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)
All values are set to 0
and will therefore map to the first color in your colorscale
. The same for the next color.
In addition you need to set cmax
and cmin
manually.
Complete code
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)
Thanks! That's a great example. I'm not sure this warrants a separate question, but do you know why Plotly doesn't display overlaps correctly with transparency? It seems to suffer from the same problems as matplot3d. That is, even at 99% opacity, whichever trace is added to data last appears in front. Only at 100% opacity do the overlaps show correctly
– Jake Stevens-Haas
Jan 2 at 16:19
@JakeStevens-Haas: No idea. I'd guess it would be best to open an issue on Github.
– Maximilian Peters
Jan 2 at 17:45
add a comment |
The documentation is a bit cryptic here.
surfacecolor
(list, numpy array, or Pandas series of numbers, strings, or datetimes.)
Sets the surface color values, used for setting a color scale independent of
z
.
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)
All values are set to 0
and will therefore map to the first color in your colorscale
. The same for the next color.
In addition you need to set cmax
and cmin
manually.
Complete code
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)
The documentation is a bit cryptic here.
surfacecolor
(list, numpy array, or Pandas series of numbers, strings, or datetimes.)
Sets the surface color values, used for setting a color scale independent of
z
.
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)
All values are set to 0
and will therefore map to the first color in your colorscale
. The same for the next color.
In addition you need to set cmax
and cmin
manually.
Complete code
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)
answered Jan 1 at 10:19
Maximilian PetersMaximilian Peters
15.2k63150
15.2k63150
Thanks! That's a great example. I'm not sure this warrants a separate question, but do you know why Plotly doesn't display overlaps correctly with transparency? It seems to suffer from the same problems as matplot3d. That is, even at 99% opacity, whichever trace is added to data last appears in front. Only at 100% opacity do the overlaps show correctly
– Jake Stevens-Haas
Jan 2 at 16:19
@JakeStevens-Haas: No idea. I'd guess it would be best to open an issue on Github.
– Maximilian Peters
Jan 2 at 17:45
add a comment |
Thanks! That's a great example. I'm not sure this warrants a separate question, but do you know why Plotly doesn't display overlaps correctly with transparency? It seems to suffer from the same problems as matplot3d. That is, even at 99% opacity, whichever trace is added to data last appears in front. Only at 100% opacity do the overlaps show correctly
– Jake Stevens-Haas
Jan 2 at 16:19
@JakeStevens-Haas: No idea. I'd guess it would be best to open an issue on Github.
– Maximilian Peters
Jan 2 at 17:45
Thanks! That's a great example. I'm not sure this warrants a separate question, but do you know why Plotly doesn't display overlaps correctly with transparency? It seems to suffer from the same problems as matplot3d. That is, even at 99% opacity, whichever trace is added to data last appears in front. Only at 100% opacity do the overlaps show correctly
– Jake Stevens-Haas
Jan 2 at 16:19
Thanks! That's a great example. I'm not sure this warrants a separate question, but do you know why Plotly doesn't display overlaps correctly with transparency? It seems to suffer from the same problems as matplot3d. That is, even at 99% opacity, whichever trace is added to data last appears in front. Only at 100% opacity do the overlaps show correctly
– Jake Stevens-Haas
Jan 2 at 16:19
@JakeStevens-Haas: No idea. I'd guess it would be best to open an issue on Github.
– Maximilian Peters
Jan 2 at 17:45
@JakeStevens-Haas: No idea. I'd guess it would be best to open an issue on Github.
– Maximilian Peters
Jan 2 at 17:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53992606%2fplotly-different-color-surfaces%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown