Plots inside of existing plots

Hello,

I was wondering if there was a way to draw secondary graphs inside of or on top of a primary scatter chart. I’ve been working on a scatter chart that would display pie charts for each respective point, but I haven’t been able to get anything working yet.

I’ve seen some examples regarding drawing secondary graphs alongside a primary graph, but nothing regarding drawing a graph inside of or over a graph.

Thank you,
Claire

Hi @clairew ,

Did you mean something like this :

You can create Scatter trace first .
Second step is create another figure object with make_subplots to manage position of your pie chart inside previous scatter chart .
Add traces data from second figure (pie charts) into first figure (Scatter) by using add_traces method.

import plotly.graph_objects as go
import numpy as np
from plotly.subplots import make_subplots

N = 1000
t = np.linspace(0, 50, 100)
y = np.sin(t)


fig = go.Figure(go.Scatter(x=t, y=y, mode='markers'))

labels = ["US", "China", "European Union", "Russian Federation", "Brazil", "India",
          "Rest of World"]
fig1 = make_subplots(rows=2, cols=4, specs=[[{'type':'domain'}, {'type':'domain'}, {},{}],[{},{}, {},{}]])
fig1.add_trace(go.Pie(labels=labels, values=[16, 15, 12, 6, 5, 4, 42], name="GHG Emissions"),
              1, 1)
fig1.add_trace(go.Pie(labels=labels, values=[27, 11, 25, 8, 1, 3, 25], name="CO2 Emissions"),
              1, 2)

# Use `hole` to create a donut-like pie chart
fig1.update_traces(hole=.4, hoverinfo="label+percent+name")

fig.add_traces(fig1.data)


fig.update_layout(xaxis=dict(range=(0,50)),yaxis=dict(range=(0,2)))
fig.show()

or you can checkout this post, it seems relevant to your issue.

Hope this help.

2 Likes

Yes, thank you! Do you know if it’d be possible to layer the secondary charts on top of individual points, or is it locked to a grid based system?

Thank you,
Claire

Hi @clairew ,

As far as I know it locked by a grid based system when you mean draw a pie chart.
So, to do that you need to match the position of every points with coordinate of the grid system (subplots).