使用标志在plotly中合并图形 - Flagpy在plotly中不起作用

问题描述 投票:0回答:1

我正在尝试制作一个图形,并在每个条形图中在它旁边放置一个标志。为此,我使用

flagpy
plotly
。但它似乎并没有发挥作用。 据我所知。你可以帮帮我吗 ?这里有一个虚拟代码,还有国家/地区。

import plotly.graph_objects as go
import pandas as pd
import flagpy as fp


# fp.get_flag_img function from flagpy

# Your DataFrame setup
df_date_top_n = pd.DataFrame({
    'CTR_DSC': ['Spain', 'Canada', 'Germany'],
    'PLT_NAM_DSC': ['Plant A', 'Plant B', 'Plant C'],
    'KPI_VAL': [100, 200, 150],
    'flag_emoji': ['🇪🇸', '🇨🇦', '🇩🇪']
})

# Sorting
df_date_top_n.sort_values('KPI_VAL', ascending=True, inplace=True)

# Create the plot
fig = go.Figure()

# Add bars
fig.add_trace(go.Bar(
    x=df_date_top_n['KPI_VAL'],
    y=df_date_top_n['PLT_NAM_DSC'],
    orientation='h',
    text=df_date_top_n['KPI_VAL'],
    textposition='auto',
    opacity=0.8,
))

# If you remove this you get the function
for i, row in df_date_top_n.iterrows():
    # Correctly using CTR_DSC to fetch flag images
    flag_url = fp.get_flag_img(row['CTR_DSC'])  
    fig.add_layout_image(
        dict(
            source=Image.open(flag_url),
            xref="x", yref="y",
            x=row['KPI_VAL'] - 5,  # Adjusting this value for better visibility
            y=row['PLT_NAM_DSC'],
            sizex=0.05, sizey=0.05,  # Adjust size as necessary
            xanchor="center", yanchor="middle",
            sizing="contain",
            opacity=0.8,
            layer="above"
        )
    )

# Customize layout
fig.update_layout(
    title='Top Plant Consumption',
    xaxis_title="MWH",
    yaxis_title="Factory Name",
)

# Show the figure
fig.show()

我想要 200 旁边的通讯国国旗

df_date_top_n['CTR_DSC']
感谢您提前抽出时间,

有关更多信息,该函数输出以下内容:

img = fp.get_flag_img('Germany')
img

python-3.x plotly visualization flags flagpy
1个回答
0
投票

这对我来说很有效

source=PIL.Image.open(urllib.request.urlopen(row["flag_url"]))
,这样:

import plotly.graph_objects as go
import pandas as pd
import matplotlib.pyplot as plt


    # Assuming fp.get_flag_img function exists and works with country names or codes to return a flag image URL

    # Your DataFrame setup
    df_date_top_n = pd.DataFrame({
        'CTR_DSC': ['Spain', 'Canada', 'Germany'],
        'PLT_NAM_DSC': ['Plant A', 'Plant B', 'Plant C'],
        'KPI_VAL': [100, 200, 150],
        'flag_emoji': ['🇪🇸', '🇨🇦', '🇩🇪']
    })
    # Assuming get_country_alpha_2_code is your function that returns a two-letter country code
    df_date_top_n['flag_url'] = df_date_top_n['CTR_DSC'].apply(lambda country_name: f"https://flagcdn.com/h20/{get_country_alpha_2_code(country_name).lower()}.png")
    print(df_date_top_n)

    # Sorting
    df_date_top_n.sort_values('KPI_VAL', ascending=True, inplace=True)

    # Create the plot
    fig = go.Figure()

    # Add bars
    fig.add_trace(go.Bar(
        x=df_date_top_n['KPI_VAL'],
        y=df_date_top_n['PLT_NAM_DSC'],
        orientation='h',
        text=df_date_top_n['KPI_VAL'],
        textposition='auto',
        opacity=0.8,
    ))

    # If you remove this you get the function
    for i, row in df_date_top_n.iterrows():
        # Correctly using CTR_DSC to fetch flag images
        
        fig.add_layout_image(
            dict(
                source=PIL.Image.open(urllib.request.urlopen(row["flag_url"])),
                xref="x", yref="y",
                x=row['KPI_VAL'] - 5,  # Adjusting this value for better visibility
                y=row['PLT_NAM_DSC'],
                sizex=10, sizey=10,  # Adjust size as necessary
                xanchor="center", yanchor="middle",
                sizing="contain",
                opacity=0.8,
                layer="above"
            )
        )

    # Customize layout
    fig.update_layout(
        title='Top Plant Consumption',
        xaxis_title="MWH",
        yaxis_title="Factory Name",
    )

    # Show the figure
    fig.show()

。现在只是调整而已。

© www.soinside.com 2019 - 2024. All rights reserved.