Plotly layout with background image from local file
I have the same problem as stated in this question: I want my plot to have a background from a local png file.
Using plotly's example, which uses an image from the web, works.
Using a path to my local image, with or without absolute path, does not work.
Encoding as per the answer to this other question does not help.
import base64
import os
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import numpy as np
trace1= go.Scatter(x=[0,0.5,1,2,2.2],y=[1.23,2.5,0.42,3,1])
with open(os.getcwd() + "/resources/office_map.png", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode()
# Add the prefix that plotly will want when using the string as source
encoded_image = "data:image/png;base64," + encoded_string
layout= go.Layout(
title='My title',
images= [dict(
source= encoded_image,
xref= "x",
yref= "y",
x= 0,
y= 10,
sizex= 744,
sizey= 1052,
sizing= "stretch",
opacity= 0.5,
layer= "below")])
fig=go.Figure(data=[trace1],layout=layout)
plot(fig)
How hard can it be to get a local image as background? How do you do it?
python plotly
add a comment |
I have the same problem as stated in this question: I want my plot to have a background from a local png file.
Using plotly's example, which uses an image from the web, works.
Using a path to my local image, with or without absolute path, does not work.
Encoding as per the answer to this other question does not help.
import base64
import os
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import numpy as np
trace1= go.Scatter(x=[0,0.5,1,2,2.2],y=[1.23,2.5,0.42,3,1])
with open(os.getcwd() + "/resources/office_map.png", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode()
# Add the prefix that plotly will want when using the string as source
encoded_image = "data:image/png;base64," + encoded_string
layout= go.Layout(
title='My title',
images= [dict(
source= encoded_image,
xref= "x",
yref= "y",
x= 0,
y= 10,
sizex= 744,
sizey= 1052,
sizing= "stretch",
opacity= 0.5,
layer= "below")])
fig=go.Figure(data=[trace1],layout=layout)
plot(fig)
How hard can it be to get a local image as background? How do you do it?
python plotly
add a comment |
I have the same problem as stated in this question: I want my plot to have a background from a local png file.
Using plotly's example, which uses an image from the web, works.
Using a path to my local image, with or without absolute path, does not work.
Encoding as per the answer to this other question does not help.
import base64
import os
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import numpy as np
trace1= go.Scatter(x=[0,0.5,1,2,2.2],y=[1.23,2.5,0.42,3,1])
with open(os.getcwd() + "/resources/office_map.png", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode()
# Add the prefix that plotly will want when using the string as source
encoded_image = "data:image/png;base64," + encoded_string
layout= go.Layout(
title='My title',
images= [dict(
source= encoded_image,
xref= "x",
yref= "y",
x= 0,
y= 10,
sizex= 744,
sizey= 1052,
sizing= "stretch",
opacity= 0.5,
layer= "below")])
fig=go.Figure(data=[trace1],layout=layout)
plot(fig)
How hard can it be to get a local image as background? How do you do it?
python plotly
I have the same problem as stated in this question: I want my plot to have a background from a local png file.
Using plotly's example, which uses an image from the web, works.
Using a path to my local image, with or without absolute path, does not work.
Encoding as per the answer to this other question does not help.
import base64
import os
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import numpy as np
trace1= go.Scatter(x=[0,0.5,1,2,2.2],y=[1.23,2.5,0.42,3,1])
with open(os.getcwd() + "/resources/office_map.png", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode()
# Add the prefix that plotly will want when using the string as source
encoded_image = "data:image/png;base64," + encoded_string
layout= go.Layout(
title='My title',
images= [dict(
source= encoded_image,
xref= "x",
yref= "y",
x= 0,
y= 10,
sizex= 744,
sizey= 1052,
sizing= "stretch",
opacity= 0.5,
layer= "below")])
fig=go.Figure(data=[trace1],layout=layout)
plot(fig)
How hard can it be to get a local image as background? How do you do it?
python plotly
python plotly
edited Nov 22 at 14:02
asked Nov 22 at 13:33
Gauthier
22.5k94269
22.5k94269
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I needed to read about the x
and y
parameters of the images
dictionary. I had changed the y
before testing the encoding solution, and the image appeared way outside the plot. How silly. Setting y
to 3 instead fixes it.
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%2f53432151%2fplotly-layout-with-background-image-from-local-file%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
I needed to read about the x
and y
parameters of the images
dictionary. I had changed the y
before testing the encoding solution, and the image appeared way outside the plot. How silly. Setting y
to 3 instead fixes it.
add a comment |
I needed to read about the x
and y
parameters of the images
dictionary. I had changed the y
before testing the encoding solution, and the image appeared way outside the plot. How silly. Setting y
to 3 instead fixes it.
add a comment |
I needed to read about the x
and y
parameters of the images
dictionary. I had changed the y
before testing the encoding solution, and the image appeared way outside the plot. How silly. Setting y
to 3 instead fixes it.
I needed to read about the x
and y
parameters of the images
dictionary. I had changed the y
before testing the encoding solution, and the image appeared way outside the plot. How silly. Setting y
to 3 instead fixes it.
answered Nov 22 at 14:12
Gauthier
22.5k94269
22.5k94269
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53432151%2fplotly-layout-with-background-image-from-local-file%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