Ausgabe
einfach, aber viel Zeit damit verschwendet, dies zu tun. Ich möchte das Attribut color_continuous_scale=”ylgn” des Diagramms fig = px.choropleth_mapbox ändern. Die Handlung funktioniert, aber wenn ich auf meine Dropdown-Schaltflächen klicke, ändert sie sich überhaupt nicht, wenn ich die Dropdown-Schaltflächen ausprobiere, nicht auf Temps oder eine der anderen eingebauten Farbskalen (die letzten 2 der Dropdown-Liste dienen eher zum Testen), wie kann ich ändern die Farbskala (mithilfe meiner Dropdown-Schaltflächen) von ylgn bis temps oder andere wie viridis grey halin ….?
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_mapbox(df, geojson=counties, locations='fips', color='unemp',
color_continuous_scale="ylgn",
range_color=(0, 12),
mapbox_style="carto-positron",
zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
opacity=0.5,
labels={'unemp':'unemployment rate'}
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.update_layout(
updatemenus=[
dict(
active=0,
buttons=list([
dict(label="1",
method="update",
args=["colorscale", "ylgn"]),
dict(label="2",
method="update",
args=["colorscale", "temps"]),
dict(label="3",
method="update",
args=[{"range_color": (0, 500)}]),
dict(label="4",
method="update",
args=[{"range_color": (0, 500)}]),
]),
)
])
# Set title
fig.update_layout(title_text="Map of the USA")
fig.show()
Lösung
Wenn Sie eine einfache Karte mit einem Diagrammobjekt erstellen, können Sie die Farbkarte mit Schaltflächen umschalten. Ich habe keinen Grund zu erklären, warum dies nicht in Express möglich ist. Die Schaltfläche zum Umschalten der Farbtabelle in den Referenzen geht zurück zum ersten Farbtabellensatz, aber in meiner Umgebung (jupyterlab) kehrt sie nicht zur Standardfarbtabelle zurück.
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.graph_objects as go
fig = go.Figure(go.Choroplethmapbox(geojson=counties, locations=df.fips, z=df.unemp,
colorscale="ylgn", zmin=0, zmax=12,
marker_opacity=0.5, marker_line_width=0))
fig.update_layout(mapbox_style="carto-positron",
mapbox_zoom=3, mapbox_center = {"lat": 37.0902, "lon": -95.7129})
fig.update_layout(margin={"r":0,"t":30,"l":0,"b":0})
fig.update_layout(
updatemenus=[
dict(
active=0,
buttons=list([
dict(label="ylgn",
method="restyle",
args=["colorscale", "ylgn"]),
dict(label="temps",
method="restyle",
args=["colorscale", "temps"]),
]),
showactive=True,
#type='buttons'
)
])
# Set title
fig.update_layout(title_text="Map of the USA")
fig.show()
Beantwortet von – r-Anfänger
Antwort geprüft von – David Goodson (FixError Volunteer)