janinedk
janinedk

Reputation: 13

How can I modify this map with plotly in Python?

I use this Dataset:

https://github.com/selva86/datasets/blob/master/supermarket_sales.csv

In this Dataset you can see the cities: Yangon, Naypitaw and Mandalay Second, you can see the Quantity of each sold product in this city. I want to create a Bubble Map for my Dashboard. I have this Code from Plotly, but I don't know how to change this, so this bubble map shows me bubbles for this 3 cities and the bubble-size depends on how many products were sold in this city.

I would be very happy if someone can help me.

This is the code I have from plotly.com:

import plotly.express as px
df = px.data.gapminder().query("year==2007")
fig = px.scatter_geo(df, locations="iso_alpha", color="continent",
                     hover_name="country", size="pop",
                     projection="natural earth")
fig.show()

(https://plotly.com/python/bubble-maps/)

Thank you for your help.

Upvotes: 0

Views: 356

Answers (2)

hoa tran
hoa tran

Reputation: 1711

Based on @Berlin Benilo df1, you can also use below code to create Bubble Maps:

px.set_mapbox_access_token('pk.eyJ1IjoiY292aWRwcm9qZWN0IiwiYSI6ImNsMW95Y2Z6MTE2NW0zZG8ybTZjbWlha3YifQ.xaeH7tKRNdRnevWw2uZ70Q')
fig = px.scatter_mapbox(df1,
                          lat=df1["lat"],
                          lon=df1["long"],
                          size="Quantity",
                          color='Quantity',
                          #color_continuous_scale=px.colors.sequential.Viridis,
                          zoom=5,
                          hover_name="Quantity")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

And here is the result: [https://imgur.com/a/rCvI89S]

Upvotes: 0

Berlin Benilo
Berlin Benilo

Reputation: 502

I had tried with the following code and I hope this solution might be useful for you. Initially after reading the input file as a dataframe . I grouped the dataframe with respect to city column and proceed with on.

import pandas as pd
import plotly.graph_objects as go

df = pd.read_csv('supermarket_sales.csv')
df1 = df.groupby('City').sum().reset_index()
df1['lat'] = [21.9588,16.8409,19.7633]
df1['long']= [96.0891,96.1735,96.0785]

Normally for a geo-plot, the latitude and longitude field is mandatory in most of the cases. So i manually loaded those fields for our case.

colors = ['rgb(255,0,255)','rgb(255,0,0)','rgb(0,0,255)']
fig = go.Figure(go.Scattergeo())
fig.update_geos(
    visible=False, resolution=110, scope="asia",
    showcountries=True, countrycolor="Black",
    showsubunits=True, subunitcolor="Blue",fitbounds='locations'
)

fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0})
fig.add_trace(go.Scattergeo(
    locationmode = 'country names',
    lon = df1['long'],
    lat = df1['lat'],
    text = df1['City'],
    mode = 'markers',
    marker = dict(
                size = df1['Quantity'],
                color = colors,
                line_width = 0,
                sizeref = 9,
                sizemode = "area",
                reversescale = True
            )))

fig.show()

the output graph is, enter image description here

The reason for every region were on same size is because of each country serving nearby same quantity sum. I hope this will useful for you. Happy coding :).

Upvotes: 1

Related Questions