亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Plotly Express:您可以在 px.choropleth 中手動定義圖例嗎?

Plotly Express:您可以在 px.choropleth 中手動定義圖例嗎?

慕雪6442864 2023-03-30 17:11:24
我正在制作px.choropleth()圖表plotly.express。這是代碼片段;import psycopg2import pandas as pdimport plotly.express as px''' PostgreSQL Variables '''# your postgres login variables''' PostgreSQL Connection '''# your postgres connection code''' SQL Query '''# your SQL Query ''' Load SQL Queries into Pandas DataFrame '''African = pd.DataFrame(SQL_Query_Code,                       columns=['list-of-all-columns'])''' Variable for Personal Colours '''# e.g. HTML/HEx code : '#4c5c73'# List all of the shades/tints codes; in this example I am using the tints codesmy_color_scale = [[0.0, '#4c5c73'], [0.1, '#5D6C81'], [0.2, '#6F7C8F'], [0.3, '#818C9D'], [0.4, '#939DAB'],                  [0.5, '#A5ADB9'], [0.6, '#B7BDC7'], [0.7, '#C9CED5'], [0.8, '#DBDEE3'], [0.9, '#EDEEF1'],                  [1.0, '#FFFFFF']]''' Plotly graph '''# Government Respond - School Closingafrican_figure1 = px.choropleth(African,                                locations='countrycode',                                color='c1_school_closing',                                color_continuous_scale=my_color_scale,                                range_color=[0, 3],                                hover_data={'c1_school_closing': False,                                            'countrycode': False,                                            'countryname': False},                                hover_name='countryname',                                labels={'c1_school_closing': 'SCALE'})是否可以手動重命名由 ? 自動創建的圖例px.choropleth?
查看完整描述

1 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

我舉個例子。coloraxis_colorbar您可以在更新布局中使用字典。


這里,


tickvals- 顏色條中的枚舉值


ticktext- 根據值顯示的文本


from urllib.request import urlopen

import json



with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:

    counties = json.load(response)


import pandas as pd

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",

                   dtype={"fips": str})


import plotly.express as px


fig = px.choropleth(df, geojson=counties, locations='fips', color='unemp',

                           color_continuous_scale="Viridis",

                           range_color=(0, 12),

                           scope="usa"

                          )

fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0},coloraxis_colorbar=dict(

    title="unemployment rate",

    thicknessmode="pixels",

    lenmode="pixels",

    yanchor="top",y=1,

    ticks="outside",

    tickvals=[0,4,8,12],

    ticktext=["Low", "Low Medium", "High Medium", "High"],

    dtick=4

))

fig.show()

http://img1.sycdn.imooc.com//6425527300011af806570409.jpg

現在來解決你的問題,

使用tickvals=[0,1,2,3]

ticktext=['0 - no measures', '1 - recommend closing', '2 - require closing (only some levels)', '3 - require closing all levels']


查看完整回答
反對 回復 2023-03-30
  • 1 回答
  • 0 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號