Charts and Tables
A Python file in the code editor can show charts and tables from the libraries you already know: plotly, matplotlib, pandas and Altair. Press Run and they appear in the preview beside your code, one below the other, in the order your program shows them. fig.show(), plt.show() and display(...) work as they do in a notebook.
Libraries that are not built in install with one line at the top of the file, !pip install plotly. See Installing Python Packages.
plotly
!pip install plotly
import plotly.graph_objects as go
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
fig = go.Figure()
fig.add_trace(go.Scatter(x=days, y=[12, 19, 7, 15, 22], name="Lemonade", mode="lines+markers"))
fig.add_trace(go.Scatter(x=days, y=[5, 8, 11, 9, 14], name="Cookies", mode="lines+markers"))
fig.update_layout(title="Sold this week", height=380)
fig.show()
The chart is interactive: hover for values, drag to zoom, double-click to reset. Plotly Express (import plotly.express as px) works too.
matplotlib
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
cups = [12, 19, 7, 15, 22]
plt.figure(figsize=(6, 3.5))
plt.bar(days, cups, color="#f59e0b")
plt.title("Cups of lemonade")
plt.ylabel("Cups")
plt.show()
matplotlib and pandas need no install. A figure you made but never showed is shown when your program's top level has run, as in a notebook.
pandas tables: display()
display(x) shows x in the richest form it has. A pandas DataFrame becomes a table.
import pandas as pd
df = pd.DataFrame({
"day": ["Mon", "Tue", "Wed", "Thu", "Fri"],
"cups": [12, 19, 7, 15, 22],
"cookies": [5, 8, 11, 9, 14],
})
display(df)
display(df.describe()) shows the summary table the same way.
Altair
!pip install altair
import altair as alt
import pandas as pd
df = pd.DataFrame({"day": ["Mon", "Tue", "Wed", "Thu", "Fri"], "cups": [12, 19, 7, 15, 22]})
chart = alt.Chart(df).mark_bar(color="#0ea5e9").encode(x=alt.X("day", sort=None), y="cups")
chart.properties(width=420, height=220, title="Cups of lemonade").show()
Altair charts load their drawing library from the internet, so they need a connection.
display() and its friends
| Call | What it does |
|---|---|
display(x, y, ...) | Shows each one, below what is already shown |
display(x, id="total") | Shows x, replacing what was shown with the same id. Use it for a chart that updates |
clear_output() | Removes everything shown so far |
Anything can be displayed: a picture (Pillow) shows as an image, and anything else as its printed form.
A chart that updates
Give the chart an id and display it again whenever the data changes:
!pip install plotly
import plotly.graph_objects as go
import random
values = []
async def main():
for _ in range(20):
values.append(random.randint(1, 10))
display(go.Figure(go.Scatter(y=values)), id="live")
await sleep(0.5)
await main()
await sleep(0.5) waits half a second while the chart stays on screen. Stop ends the loop.
Good to know
- An output that is a small web page of its own runs in a frame of its own, apart from the rest of Circuitry.
- plotly, matplotlib and tables work offline once their packages are installed.
- A file that shows charts is Circuitry's: File → Export as Web Page does not include them.