Visualization workflows#
This notebook shows the new unified db.plot(...) workflow on the packaged MARIO test databases.
The same plotting engine supports both a guided path for quick inspection and an advanced path with explicit Plotly Express mappings, including custom palettes.
[1]:
import mario
Load the packaged test databases#
These are the same small MARIO-readable tables used elsewhere in the documentation and test suite, so the examples are reproducible without external downloads.
[2]:
iot = mario.load_test("IOT")
sut = mario.load_test("SUT")
INFO Parser: excel reading IOT flows from /Users/lorenzorinaldi/Documents/GitHub/MARIO/mario/test/tables/test_IOT_standard.xlsx.
INFO Parser: state payload ready with 6 canonical blocks.
INFO Parser: excel state ready for IOT.
INFO Metadata: initialized.
INFO Parser: excel reading SUT flows from /Users/lorenzorinaldi/Documents/GitHub/MARIO/mario/test/tables/test_SUT_standard.xlsx.
INFO Parser: state payload ready with 10 canonical blocks.
INFO Parser: excel state ready for SUT.
INFO Metadata: initialized.
Quick path: plot one matrix with a preset#
For a first look at a matrix, pass the matrix name and let MARIO choose a useful Plotly Express mapping with a preset such as "overview".
[3]:
fig = iot.plot(
matrix="Z",
preset="overview",
auto_open=False,
path=False,
)
fig
Inspect the dataframe actually plotted#
Set return_data=True when you want both the Plotly figure and the dataframe passed to Plotly Express after filtering, aggregation and top-n trimming.
[4]:
fig, plotted = iot.plot(
matrix="Z",
preset=None,
kind="bar",
x="Sector_from",
color="Region_to",
auto_open=False,
path=False,
return_data=True,
)
plotted.head()
[4]:
| Sector_from | Region_to | Value | |
|---|---|---|---|
| 0 | Agriculture | Reg1 | 7.367798e+06 |
| 1 | Agriculture | Reg2 | 1.048720e+05 |
| 2 | Industry | Reg1 | 3.487216e+07 |
| 3 | Industry | Reg2 | 6.248561e+05 |
| 4 | Services | Reg1 | 4.228452e+07 |
Advanced path: explicit Plotly Express mappings#
More experienced users can control the chart directly through x, color, facet_col, animation_frame, agg, top_n and the other familiar mappings.
[5]:
fig = iot.plot(
matrix="Y",
kind="bar",
preset=None,
x="Sector_from",
color="Consumption category_to",
facet_col="Region_to",
agg="sum",
top_n=6,
auto_open=False,
path=False,
)
fig
Filter before plotting#
Use filters={...} to keep only the levels you want before MARIO aggregates the data.
[6]:
fig = iot.plot(
matrix="Z",
kind="bar",
preset=None,
x="Sector_from",
color="Sector_to",
filters={"Region_from": ["Reg1"], "Region_to": ["Reg1"]},
top_n=4,
auto_open=False,
path=False,
)
fig
SUT-specific views#
The same engine works on split SUT blocks such as U and S, so you can plot activity and commodity dimensions directly.
[7]:
fig = sut.plot(
matrix="U",
kind="bar",
preset=None,
x="Commodity_from",
color="Activity_to",
facet_col="Region_to",
top_n=4,
auto_open=False,
path=False,
)
fig
Plot region-by-region trades for one item#
db.calc_trades(...) can aggregate intermediate and final trade into one region-by-region matrix. Use total=True to add row and column totals.
[8]:
trade_matrix = iot.calc_trades(
"Agriculture",
total=True,
)
trade_matrix
[8]:
| Region | Reg1 | Reg2 | Total |
|---|---|---|---|
| Region | |||
| Reg1 | 9.897401e+06 | 74768.097334 | 9.972169e+06 |
| Reg2 | 8.933711e+03 | 70915.947332 | 7.984966e+04 |
| Total | 9.906335e+06 | 145684.044667 | 1.005202e+07 |
Set aggregate=False and show_plot=True to keep intermediate and final trade separate and produce a heatmap with one panel per component. The plot is displayed as a side effect; the return value remains the trade matrix.
[16]:
trade_components = iot.calc_trades(
"Agriculture",
aggregate=False,
show_plot=True,
path=False,
auto_open=False,
)
trade_components
[16]:
| Intermediate | Final | |||
|---|---|---|---|---|
| Region | Reg1 | Reg2 | Reg1 | Reg2 |
| Region | ||||
| Reg1 | 7.363942e+06 | 63312.752520 | 2.533459e+06 | 11455.344815 |
| Reg2 | 3.855907e+03 | 41559.247333 | 5.077805e+03 | 29356.699999 |
Plot a prepared dataframe#
db.plot(...) can also accept a custom dataframe when the quantity to visualize is not one raw MARIO matrix. Here we reuse sectoral GDP.
[10]:
gdp = iot.GDP(total=False).reset_index()
gdp
[10]:
| Region | Sector | GDP | |
|---|---|---|---|
| 0 | Reg1 | Agriculture | 6.016424e+06 |
| 1 | Reg1 | Industry | 1.497569e+07 |
| 2 | Reg1 | Services | 6.206624e+07 |
| 3 | Reg2 | Agriculture | 4.539138e+04 |
| 4 | Reg2 | Industry | 3.520592e+05 |
| 5 | Reg2 | Services | 1.428250e+06 |
[11]:
fig = iot.plot(
data=gdp,
kind="treemap",
preset=None,
y="GDP",
path_columns=["Sector"],
auto_open=False,
path=False,
)
fig
Change the default palette#
Use mario.set_palette(...) to change the default discrete color sequence used by db.plot(...). MARIO ships built-in palettes such as mario, Plotly, Safe, Vivid, Pastel, Alphabet, and McKinsey.
[12]:
mario.set_palette("Safe")
fig = iot.plot(
matrix="Y",
kind="bar",
preset=None,
x="Sector_from",
color="Consumption category_to",
facet_col="Region_to",
agg="sum",
top_n=6,
auto_open=False,
path=False,
)
fig
You can also pass an explicit list of colors. The palette is session-wide, so reset it when you want to go back to the MARIO defaults.
[13]:
mario.set_palette("mario")
fig = iot.plot(
matrix="Z",
kind="sunburst",
preset=None,
path_columns=["Region_from", "Sector_from"],
top_n=8,
auto_open=False,
path=False,
)
fig