plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
16.11k stars 2.54k forks source link

empty fig using new px.choropleth_map (even with code example) #4751

Closed lopezj1 closed 1 month ago

lopezj1 commented 1 month ago

Using version 5.24.0. New px.choropleth_map was not working for me with my data, so decided to run the code snippet from the plotly example. This also did not work in jupyter notebook.

Source: Example

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_map(df, geojson=counties, locations='fips', color='unemp',
                           color_continuous_scale="Viridis",
                           range_color=(0, 12),
                           map_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()

Output:

px choropleth_map-output
archmoj commented 1 month ago

@LiamConnors Would you please have a look at this and possibly create a plotly.js codepen?

LiamConnors commented 1 month ago

@lopezj1 what environment are you running the jupyter notebook in? Is it a Jupyter notebook within an IDE like VS Code? What version of Plotly.js do you see when you hover over the plotly logo in the top right corner of the empty fig that renders for you.

image
lopezj1 commented 1 month ago

@LiamConnors I am running an iteractive window within VSCode and this shows Plotly.js (v2.29.1).

But print(plotly.__version__) shows plotly 5.24.0 is being used

I used uv pip install --upgrade plotly to upgrade to 5.24.0

px choropleth_map-ide
LiamConnors commented 1 month ago

@lopezj1 VS Code uses its only bundled Plotly.js, so sometimes the latest features won't work immediately there. This page in the docs gives a bit more detail https://plotly.com/python/troubleshooting/#vscode-notebook-nteract-and-streamlit-problems

Setting the renderer on fig.show to "notebook" is a workaround if you see it's not using the latest Plotly.js

fig.show(renderer="notebook")

lopezj1 commented 1 month ago

@LiamConnors oh ok that makes sense. Actually I wanted this to work in streamlit, which it didn't, so tried the vscode notebook. But looks like both are affected per the link.

Is there an alternate renderer for streamlit?