[英]Plotly: How to plot specific rows on Bubble Map?
如何使用 Plotly 對 ZA7F5F35426B5628741 使用 Map plot 某個狀態的大小? (即:plot 僅限西海岸和東海岸州)
我看過 Plotly 的例子,但沒有給出太多解釋如何重現相同的 plot (https://plotly.com/python/bubble-maps/#united-states-bubble-map )
樣本數據:
state latitude longitude mag
0 Alabama 34.7360 -85.629000 4.8
1 Alaska 71.1404 -131.232000 7.9
2 Arizona 36.9940 -109.058700 5.3
3 Arkansas 36.4400 -89.920000 5.0
4 California 41.9780 -115.366000 7.3
我的代碼:
every_earthquake = pd.read_csv('earthquake_states.csv')
fifty_state_quakes = every_earthquake[['state', 'latitude', 'longitude', 'mag']].dropna()
state_mag_max = pd.DataFrame(fifty_state_quakes, columns=['state',
'latitude', 'longitude', 'mag']).dropna()
state_max_mag_df = state_mag_max.groupby(['state'])['latitude', 'longitude','mag'].max().reset_index()
state_max_mag_df['text'] = state_max_mag_df['state'] + '<br>Magnitude ' + (state_max_mag_df['mag']).astype(str)
magnitude = [(0,1), (2,3), (4,5), (5,6), (7,8), (9, 10)]
colors = ['royalblue','crimson','lightseagreen','orange','lightgrey']
scale = 0.45
fig = go.Figure()
for i in range(len(magnitude)):
magn = magnitude[i]
state_max_mag_df1 = state_max_mag_df[(state_max_mag_df['mag'] >= magn[0]) & (state_max_mag_df['mag'] < magn[1])]
fig.add_trace(go.Scattergeo(
locationmode = 'USA-states',
lon = state_max_mag_df['latitude'],
lat = state_max_mag_df['latitude'],
text = state_max_mag_df['text'],
marker = dict(
size = state_max_mag_df['mag']/scale,
color = colors,
line_color = 'rgb(40,40,40)',
line_width = 0.5,
sizemode = 'area'),
name = '{0} - {1}'.format(magn[0], magn[1])))
fig.update_layout(
showlegend = True,
geo = dict(scope = 'usa',
landcolor = 'rgb(217, 217, 217)',
))
fig.show()
Output 我得到:
import pandas as pd
import plotly.graph_objects as go
every_earthquake = pd.read_csv('earthquake_states.csv')
fifty_state_quakes = every_earthquake[['state', 'latitude', 'longitude', 'mag']].dropna()
state_mag_max = pd.DataFrame(fifty_state_quakes, columns=['state', 'latitude', 'longitude', 'mag']).dropna()
state_max_mag_df = state_mag_max.groupby(['state'])['latitude', 'longitude','mag'].max().reset_index()
state_max_mag_df['text'] = state_max_mag_df['state'] + '<br>Magnitude ' + (state_max_mag_df['mag']).astype(str)
magnitude = [(0,1), (2,3), (4,5), (5,6), (7,8), (9, 10)]
colors = ['#E58606', '#5D69B1', '#52BCA3', '#99C945', '#CC61B0', '#24796C']
scale = 5
fig = go.Figure()
for i in range(len(magnitude)):
magn = magnitude[i]
state_max_mag_df1 = state_max_mag_df[(state_max_mag_df['mag'] >= magn[0]) & (state_max_mag_df['mag'] < magn[1])]
fig.add_trace(go.Scattergeo(
locationmode = 'USA-states',
lon = state_max_mag_df1['longitude'],
lat = state_max_mag_df1['latitude'],
text = state_max_mag_df1['text'],
marker = dict(
size = state_max_mag_df1['mag'] * scale,
color = colors[i],
line_color = 'rgb(40,40,40)',
line_width = 0.5,
sizemode = 'diameter'),
name = '{0} - {1}'.format(magn[0], magn[1])))
fig.update_layout(
showlegend = True,
geo = dict(scope = 'usa', landcolor = 'rgb(217, 217, 217)')
)
fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.