How to Draw Pie Chart in this Subplots with PyplotJS?

Hi all,

I see this source
https://plotly.com/julia/mixed-subplots/

This is my code (the pie chart is not working):

using PlotlyJS, CSV, DataFrames, HTTP

df = CSV.read("/home/browni/LasthrimProjection/csv/countrieshistory_db.csv", DataFrame)

# Initialize figure with subplots
fig = make_subplots(
    rows=2, cols=2,
    column_widths=[0.6, 0.4],
    row_heights=[0.4, 0.6],
    specs=[
        Spec(kind="geo", rowspan=2) Spec(kind="xy")
        missing Spec(kind= "scene")
    ]
)


# TODO: Plot not staying in it's sub plot spot
# Add scattergeo globe map of Central Bank locations
add_trace!(
    fig,
    scattergeo(
        lat=df[!, "Latitude"],
        lon=df[!, "Longitude"],
        mode="markers",
        hoverinfo="text",
        showlegend=false,
        marker=attr(color="crimson", size=4, opacity=0.8),
        projection_type="orthographic",
        landcolor="white",
        oceancolor="MidnightBlue",
        showocean=true,
        lakecolor="LightBlue"
    ),
    row=1, col=1
)

# Add locations bar chart

add_trace!(
    fig,
    bar(df, x=:Country, y=:Type, name="Type of Events"),
    row=1, col=2
)

add_trace!(
    fig,
    pie(df, values=:Type, labels=:Type, textposition="inside", texttemplate = "%{label}: %{value:\$,s} <br>(%{percent})", textfont_size=14),
    row=1, col=2
)


# Set theme, margin, and annotation in layout
relayout!(
    fig,
    geo=attr(projection_type="orthographic",
    landcolor="white",
    oceancolor="MidnightBlue",
    showocean=true,
    lakecolor="LightBlue"),
    xaxis2_tickangle=45,
    template=templates.plotly_dark,
    margin=attr(r=10, t=25, b=40, l=60),
    annotations=[
        attr(
            text="Source: World Bank, Wikipedia",
            showarrow=false,
            xref="paper",
            yref="paper",
            x=0,
            y=0)
    ]
)

fig

This is my csv, I do not use volcano data, instead I want to plot historical events from a lot of countries.

Number,Event,Country,Region,Type,Year,Latitude,Longitude
1,The Tiananmen Square protests known in Chinese as the June Fourth Incident. The protests started on 15 April and were forcibly suppressed on 4 June when the government declared martial law and sent the People's Liberation Army to occupy parts of central Beijing,China,Beijing,Politics,1989,39.7431699,116.3905867
2,The United States detonated two atomic bombs over the Japanese cities of Hiroshima and Nagasaki on 6 and 9 August 1945,Japan,Hiroshima,War,1945,35.5664783,139.5739671
3,The Treaty of Westphalia ensured Switzerland legal independence from the Holy Roman Empire,Switzerland,Bern,Politics,1648,46.8590494,5.5158505

The question is:

  1. I want to plot a pie chart, but do not know how. The pie chart will plot how many type of events in the whole database, Type: Politics, War.
  2. How to create a dropdown for the Year? So when 1989 is selected then it will show the event occurs in 1989 in the globe map.
  3. Besides a pie chart, I want to plot a scatter plot, for the (x,y) → (latitude,longitude) and the color should represent the type of events (War ->Red, Politics → Orange, Economics → Green)

Here is the picture :

For your topic question of how to obtain the pie chart, the following changes makes it work for me: instead of

    specs=[
        Spec(kind="geo", rowspan=2) Spec(kind="xy")
        missing Spec(kind= "scene")
    ]

do this

    specs=[
        Spec(kind="scattergeo", rowspan=2) Spec(kind="bar")
        missing Spec(kind="pie")
    ]

and instead of

pie(df, values=:Type, labels=:Type, textposition="inside", texttemplate = "%{label}: %{value:\$,s} <br>(%{percent})", textfont_size=14),
    row=1, col=2

do this

    pie(df, labels=:Type, textposition="inside", texttemplate = "%{label}: %{value:\$,s} <br>(%{percent})", textfont_size=14),
    row=2, col=2

In the second, I’ve removed the values=:Type as it’s not necessary, and row=1 is now row=2 as I assume you don’t want to overlay the bar chart.

Full code:
using PlotlyJS, CSV, DataFrames, HTTP

df = CSV.read("countrieshistory_db.csv", DataFrame)

# Initialize figure with subplots
fig = make_subplots(
    rows=2, cols=2,
    column_widths=[0.6, 0.4],
    row_heights=[0.4, 0.6],
    specs=[
        Spec(kind="scattergeo", rowspan=2) Spec(kind="bar")
        missing Spec(kind="pie")
    ]
)


# TODO: Plot not staying in it's sub plot spot
# Add scattergeo globe map of Central Bank locations
add_trace!(
    fig,
    scattergeo(
        lat=df[!, "Latitude"],
        lon=df[!, "Longitude"],
        mode="markers",
        hoverinfo="text",
        showlegend=false,
        marker=attr(color="crimson", size=4, opacity=0.8),
        projection_type="orthographic",
        landcolor="white",
        oceancolor="MidnightBlue",
        showocean=true,
        lakecolor="LightBlue"
    ),
    row=1, col=1
)

# Add locations bar chart

add_trace!(
    fig,
    bar(df, x=:Country, y=:Type, name="Type of Events"),
    row=1, col=2
)

add_trace!(
    fig,
    pie(df, labels=:Type, textposition="inside", texttemplate = "%{label}: %{value:\$,s} <br>(%{percent})", textfont_size=14),
    row=2, col=2
)


# Set theme, margin, and annotation in layout
relayout!(
    fig,
    geo=attr(projection_type="orthographic",
    landcolor="white",
    oceancolor="MidnightBlue",
    showocean=true,
    lakecolor="LightBlue"),
    xaxis2_tickangle=45,
    template=templates.plotly_dark,
    margin=attr(r=10, t=25, b=40, l=60),
    annotations=[
        attr(
            text="Source: World Bank, Wikipedia",
            showarrow=false,
            xref="paper",
            yref="paper",
            x=0,
            y=0)
    ]
)

display(fig)
1 Like

Thanks for your help @jd-foster
Your username almost like a famous actress. (Out of topic)

Anyway, Is there any way to show the event in the chart as well?
Maybe when I hover to the bar chart of Switzerland I can see the text of:

The Treaty of Westphalia ensured Switzerland legal independence from the Holy Roman Empire

Perhaps like embedding dataframes or something like that? Is it possible? For row 1-2 column 3

1 Like