I have a problem about not showing show Mapbox in Jupyter.
I check if it works via https://api.mapbox.com/?access_token=myaccesstoken it always returns {"api":"mapbox"}
I updated token many times but nothing changed. How can I fix it?
Here is my code snippet.
import plotly.graph_objects as go
import plotly.io as pio
import plotly.express as px
access_token = 'my-mapbox-token'
px.set_mapbox_access_token(access_token)
fig = px.density_mapbox(...)
fig.update_layout(margin={"r":0,"t":30,"l":0,"b":0})
fig.show()
Can you try to pass the token with your first update_layout() call, in which you should set your map style:
fig.update_layout(mapbox_style="dark", mapbox_accesstoken=token)
See this example for reference:
https://plotly.com/python/mapbox-layers/
-> Go to example "Dark tiles from Mapbox service.."
Related
I would like to show the lines but with some are disabled. So just like when I show it normally and then click on its name to unshow/disable the line.
I am using python.
visible attribute of a trace as "legendonly" makes a line behave in way you describe
Below code generates a figure with 10 lines, then sets visible to legendonly for lines 3 to 10. Clicking on legend makes them visible.
import pandas as pd
import numpy as np
import plotly.express as px
df = pd.DataFrame({f"line{i+1}":np.random.uniform(i,i+2,100) for i in range(10)})
px.line(df, x=df.index, y=df.columns).update_traces(visible="legendonly", selector=lambda t: not t.name in ["line1","line2"])
I am trying to plot a USA map using plotly package. Code is getting executed but nothing is getting displayed on the screen.
import plotly.graph_objs as go
import pandas as pd
data = dict(type = 'choropleth',locations = ['AZ','CA','NY'],locationmode='USA-states',colorscale='Portland',text=['text1','text2','text3']
,z=[1,0,2.0,3.0],colorbar={'title' : 'USA States Data'})
layout = dict(geo={'scope' : 'usa'})
fig = go.Figure(data=[data],layout=layout)
fig.show()
Why nothing is getting displayed and no error is also raised?
Ran your code, it's working fine for me.
(Also, try and make your title more descriptive of the problem so that if someone faces a similar problem, they can easily find your question.)
I am testing some plotly code here.
import plotly.express as px
# find business profits
pd.options.display.float_format = '{:.2f}'.format
df_gains = df_rev_exp[((df_rev_exp.ltd_spending) < df_rev_exp.REV2)]
df_gains.tail()
# scatter plot of losses
import plotly.express as px
fig = px.scatter(df_gains, x="site_name",
y="gain_or_loss",
color="gain_or_loss",
size='REV2', hover_data=['site_name','REV2'])
fig.update_xaxes(tickangle=325)
fig.show()
Everything plots just fine but the REV2 is pretty large, and as such it is hard to read when I hover over the data points in the chart. I'm trying to figure out a way to show numbers as millions. For instance, In would like to see 1.25M and not 1257789.84, which is what I am seeing now. I tried playing around with fig.update but I couldn't get anything working. How can I modify the formatting on these plotly charts?
I'm on Plotly 4.14.3 and this version shows 2.2M straight out of the box when the source is x=[10000000, 22000000, 34000000]:
import numpy as np
import plotly.graph_objects as go
fig = go.Figure()
fig.add_traces(go.Scatter(x=[10*10**6, 22*10**6, 34*10**6],
y=[10,12,14]))
fig.show()
So two things come to mind:
Update Plotly.
Check that you're inputting your values as values and not strings
I am a new user to Python. I am attempting to create a US county level chloropleth map. To get started I've been reading tutorials on how to do this here . The problem is when I execute the code exactly as it is written in the tutorial, I can't actually see the finished figure. A figure-object is created, but no plot. At the end of the day I am really looking to take this example and apply it to my own data.
Here is the code:
from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
counties = json.load(response)
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv", dtype={"fips": str})
import plotly.express as px
fig = px.choropleth_mapbox(df, geojson=counties, locations='fips', color='unemp',
color_continuous_scale="Viridis",
range_color=(0, 12),
mapbox_style="carto-positron",
zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
opacity=0.5,
labels={'unemp':'unemployment rate'})
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
In case you are running your code from a standalone script, you can set the default renderer for plotly by adding the following lines at the beginning of your script:
import plotly.io as pio
pio.renderers.default = "browser"
In this case, your default browser will be used as a renderer. Other renders are available (take a look here).
Moreover, if you are running your code in a jupyter notebook, it is enough to have fig as last command of the cell and the figure will be displayed.
I am running the following code to plot points against a city backdrop using Mapbox within Plotly in a Jupyter Notebook, but the plot does not show up, I just get a blue background.
I suspect that I am not using the token correctly?
import plotly.express as px
MBToken = 'pk.[mypublickey]'
px.set_mapbox_access_token(MBToken)
fig = px.scatter_mapbox(dfMaster.dropna()
, lat="latitude"
, lon="longitude"
, color="nta"
, size="count_of_testers"
#, color_continuous_scale=px.colors.cyclical.IceFire
#, size_max=15
#, zoom=10
)
fig.show()
#fig = px.scatter(x='latitude',y='longitude',data_frame=df)
#fig.show()
Running that gives me:
It does not appear to be a Plotly issue, the commented out code creates a scatter plot (although that has the same blue background, but the points show)
Some other posts have pointed to Jupyter offline mode being the possible culprit, but adding this did not resolve
import plotly.offline as pyo
pyo.init_notebook_mode()
Additionally, tried starting up the Jupyter notebook with a higher data rate limit as suggested, but no luck there either
This ended up being a silent data integrity error, as the size field was a string and needed to be converted into numeric