0

I have been trying every single solution that has been suggested over the Internet, but none of them seems to work for me. I get either a blank page or just the legend/colorbar for my choropleth map when I try to plot with Plotly in Colab. Can someone help me out with it? Here is a very basic code that I try to plot.

import plotly.express as px
fig = px.choropleth(gdf,
                    locations="id",
                    geojson=geojson,
                    color="Total - Visible minority",
                    scope="north america",
                    projection="conic conformal"
                    )
fig.show()
  • Is the geomerty in geojson a latitude and longitude, or is there an id field in geojson? Please refer to the examples in the [official reference](https://plotly.com/python/choropleth-maps/#choropleth-map-using-geojson) as I believe they have a similar data structure. I think I can help you if you have actual geojson and data frames. – r-beginners Apr 27 '22 at 05:38
  • My geojson file didn't have an id field, but I added based on the PRUID column of my dataframe which is specific to each province/territory. And I am still getting a blank page when I plot the map in Colab. How can I send you my files? – Farkhunda Rezaie Apr 28 '22 at 17:29
  • Can you try setting the added PRUID to LOCATIONS and see if that improves things? If the geojson file is on the net, it can be referenced, even by link. Then you can share the contents of the file using internet services available to you. – r-beginners Apr 29 '22 at 01:00

1 Answers1

0

I've encountered a couple of errors, with suggestions you may have seen:

Plotly notebook mode with google colaboratory

In particular these three possible fixes:

fig.show(renderer="colab")

Or by setting global default:

import plotly.io as pi
pio.renderers.default = "colab"

Finally, and unfortunately the possibility of having to set browser defaults in each cell by creating this function:

  def configure_plotly_browser_state():
      import IPython
      display(IPython.core.display.HTML('''
            <script src="/static/components/requirejs/require.js"></script>
            <script>
              requirejs.config({
                paths: {
                  base: '/static/base',
                  plotly: 'https://cdn.plot.ly/plotly-latest.min.js?noext',
                },
              });
            </script>
            '''))

And then running configure_plotly_browser_state() in the cell with fig.show()

Not a beautiful solution I know, but hopefully they work for you.