Search code examples
pythonplotlyplotly-pythonchoropleth

Empty graph shown when using iplot from chart_studio insted of plotly


I am following a python tutorial about the use of plotly.

Here are some commands I have to run to import functions and methods I will use

import plotly.plotly as py
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)

However, when I run commands on my jupyter nootbook, it says the use of plotly is deprecated and it recommends me to use the module chart_studio instead (error points to line import plotly.plotly as py):

ImportError: The plotly.plotly module is deprecated, please install the chart-studio package and use the chart_studio.plotly module instead.

So I run

pip install chart_studio

and try to replace the line above with functions and methods coming from the chart_studio module.

Here is my code:

import chart_studio.plotly as py
import plotly.graph_objects as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)

data = dict(type= 'cloropleth', 
            locations = ['AZ','CA','NY'],
           locationmode = 'USA-states',
           colorscale = 'Portland',
           text = ['text 1','text 2','text 3'],
           z = [1,2,3],
           colorbar = {'Title':'Colorbar title goes here'})

mylayout = dict(geo={'scope':'usa'})

choromap = go.Figure(data = [data], layout=mylayout, skip_invalid=True)

iplot(choromap)

The problem is that, when running the final line iplot(choromap), I get this empty graph

enter image description here

While in the tutorial this other graph appears

enter image description here

What is wrong?

Please note that I installed cufflinks-0.17.3 plotly-4.5.4


Solution

  • SOLVED

    In my code there were 2 errors:

    at line

    type= 'cloropleth',
    

    I had the mispelled value 'cloropleth', where the correct value is 'choropleth',

    and then at line

    colorbar = {'Title':'Colorbar title goes here'})
    

    I had 'Title', where the correct key is 'title' (lowercase).

    Fixed them and now the map is correctly displayed.

    Also, it was not necessary to install chart_studio.

    So in the end the correct code is:

    import plotly.graph_objects as go
    from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
    init_notebook_mode(connected=True)
    
    data = dict(type= 'choropleth', 
                locations = ['AZ','CA','NY'],
               locationmode = 'USA-states',
               colorscale = 'Portland',
               text = ['text 1','text 2','text 3'],
               z = [1,2,3],
               colorbar = {'title':'Colorbar title goes here'})
    
    mylayout = dict(geo={'scope':'usa'})
    
    choromap = go.Figure(data = [data], layout=mylayout)
    
    iplot(choromap)