【问题标题】:Plotly:如何在气泡图上绘制特定行?
【发布时间】:2020-10-31 09:38:42
【问题描述】:

如何使用 Plotly for Python 在气泡图上绘制特定状态的大小? (即:仅绘制西海岸和东海岸州)

我查看了 Plotly 的示例,但没有太多解释如何重现相同的情节 (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()

我得到的输出:

我想在西部和东部州的数量级下实现的输出:

【问题讨论】:

  • 您的代码中似乎有一个小错误,因为您将相同的值传递给 Plotly 的纬度和经度,就像在上面的代码中您编写的 lon = state_max_mag_df['latitude']lat = state_max_mag_df['latitude'] 一样。
  • 根据 Plotly 的示例,我必须从使用 CSV 创建的数据集中获取纬度和经度列才能找到该州地震的确切坐标,对吗?如何识别坐标并相应地绘制?
  • 您的数据框中既有纬度也有经度,但您没有使用经度,您在设置lon = state_max_mag_df['latitude']lat = state_max_mag_df['latitude'] 时使用了两次纬度。
  • 哇,我为我的愚蠢道歉。我忽略了这一点。我现在添加了“经度”,但似乎其他州的大小在气泡图上没有对齐。我应该改变比例还是线宽?
  • 别担心 :) 如果你能分享一个指向你的 CSV 文件的链接,我会看看。

标签: python plotly bubble-chart


【解决方案1】:
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()

【讨论】:

    猜你喜欢
    • 2022-07-12
    • 2021-07-19
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多