Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to plot bubble maps with pie charts as the face of each bubble using Altair #3703

Open
Philliec459 opened this issue Nov 28, 2024 · 1 comment

Comments

@Philliec459
Copy link

What is your suggestion?

Hello, I use Bokeh, Pane and Altair for all of my interactive python. I would like to make a bubble map for each well in an oil field where the size of the bubble relates to total oil production and bubble is actually a pie chart showing total oil and water production at each well.

In the example below the red piece of the pie chart is oil production and the blue piece is water production.

Image

This is the bokeh code I use, but would like to use Altair to select certain wells and observe the data for the selected wells. This is my code in Bokeh:

`from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource
import numpy as np

Enable Bokeh output in the notebook

output_notebook()

Extract relevant columns and group by well, summing the production

grouped_data = (
df[['Well', 'BOPM', 'BWPM', 'X_location', 'Y_location']]
.dropna()
.groupby(['Well', 'X_location', 'Y_location'], as_index=False)
.sum()
)

Calculate total production and proportions

grouped_data['Total'] = grouped_data['BOPM'] + grouped_data['BWPM']
grouped_data['BOPM_Fraction'] = grouped_data['BOPM'] / grouped_data['Total']
grouped_data['BWPM_Fraction'] = grouped_data['BWPM'] / grouped_data['Total']

Precompute angles for the wedges

grouped_data['start_angle_oil'] = 0
grouped_data['end_angle_oil'] = grouped_data['BOPM_Fraction'] * 2 * np.pi
grouped_data['start_angle_water'] = grouped_data['end_angle_oil']
grouped_data['end_angle_water'] = 2 * np.pi

Scale the radius based on BOPM

min_radius = 0.02
max_radius = 0.04 # Maximum radius for the largest pie
grouped_data['radius'] = grouped_data['BOPM'] / grouped_data['BOPM'].max() * max_radius

Convert to a ColumnDataSource

source = ColumnDataSource(grouped_data)

Create the figure

p = figure(title="Sized Well Production with Pie Charts", width=800, height=1000, x_range=(0, 1), y_range=(0, 1))

Add oil wedges

p.wedge(
x='X_location',
y='Y_location',
radius='radius',
start_angle='start_angle_oil',
end_angle='end_angle_oil',
color="red",
legend_label="Oil",
source=source
)

Add water wedges

p.wedge(
x='X_location',
y='Y_location',
radius='radius',
start_angle='start_angle_water',
end_angle='end_angle_water',
color="blue",
legend_label="Water",
source=source
)

Display the chart

show(p)
`

Have you considered any alternative solutions?

The bokeh example shown above, but want to use Altair interactivity. I can also do this in Spotfire, but want to use python only.

@mattijn
Copy link
Contributor

mattijn commented Nov 28, 2024

Tracked by following Vega-Lite issue: vega/vega-lite#7848

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants