Skip to content

Instantly share code, notes, and snippets.

@mebaysan
Last active February 13, 2021 10:11
Show Gist options
  • Save mebaysan/f296ff7d603f973f7a6b1dff39c69596 to your computer and use it in GitHub Desktop.
Save mebaysan/f296ff7d603f973f7a6b1dff39c69596 to your computer and use it in GitHub Desktop.
Plotly Map Scriptlerim
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import json
from unidecode import unidecode
with open('tr-cities-utf8.json','r') as file: # geojson dosyamızı açıyoruz
geojson = json.load(file)
geojson['features'][0]['id'] # ilk şehre ait id
geojson['features'][0]['properties']['name'] # ilk şehre ait isim
geodict = {}
for i in geojson['features']:
geodict[i['properties']['name']] = i['id']
sehirler.loc[:,'GeoID'] = 0
sehirler['GeoID'] = sehirler['sehir_ad'].apply(lambda x: geodict[x]) # veri setimizde ilgili şehir adı ile geojson'daki id'i match ediyoruz
##################################################################################
fig = px.choropleth_mapbox(sehirler, # hangi veri seti
geojson=geojson, # hangi geojson dosyası
locations='GeoID', # geojson dosyasında id'e denk gelen, veri setindeki hangi değişken
color='Toplam', # hangi Değişkene göre renk paleti
color_continuous_scale="Viridis", # hangi renk paleti
range_color=(sehirler['Toplam'].min(), sehirler['Toplam'].max() / 40), # renklendirme için min ve max değerler aralığı
mapbox_style="carto-positron", # mapbox stil
zoom=5, # yakınlık
center={'lat': 38.7200, 'lon': 34.0000}, # map yakınlığı
opacity=0.5, # opacity
labels={'Toplam':'Toplam Tutar'} # labellar değişecek mi
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
###############################################################
fig = px.choropleth(
sehirler, # hangi veri seti
geojson=geojson, # hangi geojson dosyası
color='Toplam', # hangi dğeişkene göre renk
locations="sehir_ad", # geojson ile eşleşecek değişken (featureidkey)
color_continuous_scale="Viridis", # renk paleti
featureidkey = 'properties.name', # geojson içindeki hangi property ile match edilecek
projection="mercator", # projeksiyon stili
range_color=(sehirler['Toplam'].min(), sehirler['Toplam'].max() / 40) # renkler için min-max değer aralığı
)
fig.update_geos(fitbounds="locations", # harita sınırları
visible=False # sınırların gözüküp gözükmemesi
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) # figure marginleri
#######################################################################################
############### Subplot ile Choropleth (Graph Objects) ###############
fig = make_subplots(
rows=1, cols=2,
specs=[
[{"type": "choropleth"}, {"type": "choropleth"}],
]
)
fig.add_trace(trace=go.Choropleth(
locations=_2019['sehir_ad'],
z = _2019['Toplam'].astype(float),
featureidkey = 'properties.name',
geojson = geojson,
colorscale = 'viridis',
colorbar_title = "Toplam",
zmin=10000,
zmax=100000,
name='2019'
),row=1,col=1)
fig.add_trace(trace=go.Choropleth(
locations=_2020['sehir_ad'],
z = _2020['Toplam'].astype(float),
featureidkey = 'properties.name',
geojson = geojson,
colorscale = 'viridis',
colorbar_title = "Toplam",
zmin=10000,
zmax=100000,
#showscale=False,
#showlegend=False,
name='2020'
),row=1,col=2)
fig.update_geos(fitbounds="locations",
visible=False,
)
hovertemp = '<i>Şehir Adı:</i> %{location} <br>'
hovertemp += '<i>Toplam:</i> %{z:,}₺'
fig.update_traces(hovertemplate=hovertemp)
#######################################################################################
#############################Scatter MapBox###############################
hovertemp = '<i>İlçe Adı:</i> %{customdata[0]} <br>'
hovertemp += '<i>Yıl:</i> %{customdata[1]} <br>'
hovertemp += '<i>Toplam Bağış:</i> %{customdata[2]:,f}₺ <br>'
hovertemp += '<i>Toplam Hesap Sayısı:</i> %{customdata[3]:,f} <br>'
fig = px.scatter_mapbox(grouped, lat="lat", lon="lon", color="Toplam", size="Toplam",
custom_data=['yerlesim_ad','Yıl','Toplam','Hesap_sayısı'],
color_continuous_scale=COLOR_SCALE, size_max=15, zoom=10)
fig.update_layout(mapbox_style="open-street-map")
fig.update_traces(hovertemplate=hovertemp)
fig.show()
##############################################################
############ Scatter MapBox with GO ###########################
fig = go.Figure(
go.Scattermapbox(
name = 'Toplamlar',
lat=grouped['lat'],
lon=grouped['lon'],
mode='markers',
marker=go.scattermapbox.Marker(
size=grouped['Toplam']/40000
),
customdata=[grouped['yerlesim_ad'],grouped['Yıl'],grouped['Toplam'],grouped['Hesap_sayısı']],
text=grouped['FigText'],
)
)
fig.update_layout(mapbox_style="open-street-map",mapbox=dict(
bearing=0,
center=go.layout.mapbox.Center(
lat=41.01384,
lon=28.94966
),
pitch=0,
zoom=10
))
########################################################################
@mebaysan
Copy link
Author

Plotly hızlı harita yapmak için bazı örnekler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment