代码之家  ›  专栏  ›  技术社区  ›  splinter

更改将鼠标悬停在plotly中的ChoroPlath贴图上时显示内容的属性

  •  6
  • splinter  · 技术社区  · 7 年前

    我正在使用 plotly 在Python 3.6.3中,我正在尝试进行Choropleth映射,如 here . 我想更改悬停在地图上方时显示内容的属性。例如,如果我们考虑加利福尼亚州的第一张地图和悬停,它看起来像:

    enter image description here

    我想更改显示内容的字体大小和框的大小。有办法访问这些吗?

    以下是生成它的代码:

    import plotly.plotly as py
    import pandas as pd
    
    df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')
    
    for col in df.columns:
        df[col] = df[col].astype(str)
    
    scl = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],\
                [0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]
    
    df['text'] = df['state'] + '<br>' +\
        'Beef '+df['beef']+' Dairy '+df['dairy']+'<br>'+\
        'Fruits '+df['total fruits']+' Veggies ' + df['total veggies']+'<br>'+\
        'Wheat '+df['wheat']+' Corn '+df['corn']
    
    data = [ dict(
            type='choropleth',
            colorscale = scl,
            autocolorscale = False,
            locations = df['code'],
            z = df['total exports'].astype(float),
            locationmode = 'USA-states',
            text = df['text'],
            marker = dict(
                line = dict (
                    color = 'rgb(255,255,255)',
                    width = 2
                ) ),
            colorbar = dict(
                title = "Millions USD")
            ) ]
    
    layout = dict(
            title = '2011 US Agriculture Exports by State<br>(Hover for breakdown)',
            geo = dict(
                scope='usa',
                projection=dict( type='albers usa' ),
                showlakes = True,
                lakecolor = 'rgb(255, 255, 255)'),
                 )
    
    fig = dict( data=data, layout=layout )
    py.iplot( fig, filename='d3-cloropleth-map' )
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   Nishnha    7 年前

    这个 chloropleth>hoverlabel 函数用于设置背景色、边框颜色和字体。但是,边框框的大小由其中的文本决定。如果名称显示为截断,则可以使用 chloropleth>hoverlabel>namelength 作用