简体   繁体   中英

Show Legend in Plotly Python

I want to show the legend of the colors assigned to each marker. When I set showlegend = True it only shows one color, and that too with a wrong label. I added the output at the last from one of their examples.

Code

    cities = [dict(
    type = 'scattergeo',
    locationmode = 'country names',
    lon = df['lng'].apply(lambda x: float(x)),
    lat = df['lat'].apply(lambda x: float(x)),
    text = df['name_of_city'],
    marker = dict(
        size = df['total_graduates'].apply(lambda x: float(x))/2000,
        color = df['effective_literacy_rate_total'],
        colorscale='Portland',
        line = dict(width=0.5, color='rgb(40,40,40)'),
        sizemode = 'area'
    )
)]
layout = dict(
        title = 'Top 500 Indian Cities by Population',
        showlegend = True,
        autosize=False,
        width=800,
        height=1000,
        geo = dict(
            resolution=100,
            scope='asia',
            lonaxis = dict( range= [ 65.0 ,100.0] ),
            lataxis = dict( range= [ 0.0,40.0 ] ),
            projection=dict( type = 'Mercator'),
            showland = True,
            landcolor = 'rgb(50,50,50)',
            subunitwidth=1,
            countrywidth=1,
            subunitcolor="rgb(255, 255, 255)",
            countrycolor="rgb(255, 255, 255)"
        ),
    )
fig = dict( data=cities, layout=layout )
iplot( fig, validate=False, filename='top-500-indian-cities' )

Output

在此处输入图片说明

How do I show the colorscale legends like this one?

在此处输入图片说明

Try changing the colorscale = 'Jet' and hiding the legend with showlegend = False .

下面代码的输出

import pandas as pd
import geopy
import plotly

#get some real data
cit = pd.read_html('https://en.wikipedia.org/wiki/List_of_cities_in_India_by_population', header=0)[0]
lat = pd.read_html('https://en.wikipedia.org/wiki/List_of_cities_by_latitude', header=0)[8]
cit = cit.merge(lat[lat.Country == 'India'], left_on='City', right_on='City')


#convert longitude and latitude
lat = list()
lon = list()
for i in cit.index:
    point = geopy.Point.from_string("{} {}".format(cit.get_value(i, 'Latitude'), cit.get_value(i, 'Longitude')))
    lat.append(point[0])
    lon.append(point[1])

cities = dict(
    type = 'scattergeo',
    locationmode = 'country names',
    lat = lat,
    lon = lon,
    text = cit['City'].tolist(),
    showlegend = False,
    marker = dict(
        size = (cit['Population (2011)[3]'] / 2000).tolist(),
        color = cit['Population (2011)[3]'].tolist(),
        colorscale = 'Jet',
        line = dict(width=0.5, color='rgb(40,40,40)'),
        sizemode = 'area',
        showscale = True
    )
)

layout = dict(
        title = 'Some Indian Cities by Population',
        showlegend = True,
        autosize = False,
        width = 800,
        height = 1000,
        geo = dict(
            resolution=100,
            scope='asia',
            lonaxis = dict( range= [ 65.0 ,100.0] ),
            lataxis = dict( range= [ 0.0,40.0 ] ),
            projection=dict( type = 'Mercator'),
            showland = True,
            landcolor = 'rgb(50,50,50)',
            subunitwidth=1,
            countrywidth=1,
            subunitcolor="rgb(255, 255, 255)",
            countrycolor="rgb(255, 255, 255)"
        ),

    )
fig = dict(data=[cities], layout=layout)
plotly.plotly.sign_in('user', 'key')
plot_url = plotly.plotly.plot(fig)


  [1]: https://i.stack.imgur.com/I9vGD.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM