Plotting Data on Your Computer
Your robot logs CSV files to the Pico's filesystem. To analyze and plot them, you download the files and run Python scripts on your computer (not the Pico). This page explains how to set up and use this workflow.
One-Time Setup
1. Check if Python is installed
Open a terminal on your computer (not the Pico REPL!) and run:
You should see something like Python 3.10.12. If you get "command not found", try python3 --version.
Installing Python
- Windows: Download from python.org. During install, check "Add Python to PATH".
- Mac:
brew install python3or download from python.org. - Linux: Usually pre-installed. If not:
sudo apt install python3 python3-pip(Debian/Ubuntu) orsudo pacman -S python(Arch).
2. Install matplotlib
If that doesn't work, try one of these:
pip3 install matplotlib # Some systems use pip3
python -m pip install matplotlib # Explicit Python module call
pip install --user matplotlib # No admin rights
3. Verify it works
You should see matplotlib OK: 3.x.x. If you get an ImportError, the install didn't work — try the alternative pip commands above.
The Workflow
Every time you want to plot data from the robot:
┌─────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐
│ Robot │────►│ Download │────►│ Plot │────►│ Analyze │
│ logs CSV │ │ to PC │ │ script │ │ the chart│
└─────────┘ └──────────┘ └──────────┘ └──────────┘
Step 1: Download the CSV from the Pico
In your terminal (not the Pico REPL):
The : prefix means "on the Pico." The . means "save to current directory on my PC."
Step 2: Save the plot script
Each tutorial provides a plot script. Save it as a .py file on your computer, for example plot_data.py.
Step 3: Run the script
A window opens with the chart. You can:
- Zoom — click the magnifying glass icon, then drag to select a region
- Pan — click the move icon, then drag
- Save — click the floppy disk icon, or the script saves a PNG automatically
- Close — close the window to return to the terminal
Minimal Plot Template
Use this as a starting point for any CSV data:
import csv
import matplotlib.pyplot as plt
# --- Load CSV ---
data = {}
with open("your_file.csv") as f:
reader = csv.DictReader(f)
for col in reader.fieldnames:
data[col] = []
for row in reader:
for col in reader.fieldnames:
data[col].append(float(row[col]))
# --- Plot ---
plt.figure(figsize=(10, 4))
plt.plot(data["time_ms"], data["error"], label="Error")
plt.xlabel("Time (ms)")
plt.ylabel("Error")
plt.title("My Robot Data")
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig("my_plot.png", dpi=150)
plt.show()
Change the column names (time_ms, error) to match your CSV headers. Add more plt.plot() lines to overlay multiple signals.
Multiple Subplots
fig, axes = plt.subplots(2, 1, figsize=(10, 6), sharex=True)
axes[0].plot(data["time_ms"], data["error"])
axes[0].set_ylabel("Error")
axes[0].set_title("Top chart")
axes[1].plot(data["time_ms"], data["pwm"], color="orange")
axes[1].set_ylabel("PWM")
axes[1].set_xlabel("Time (ms)")
plt.tight_layout()
plt.savefig("multi_plot.png", dpi=150)
plt.show()
Comparing Multiple Runs
for filename in ["run1.csv", "run2.csv", "run3.csv"]:
data = load_csv(filename) # Use the load function from above
plt.plot(data["time_ms"], data["error"], label=filename)
plt.legend()
plt.show()
Troubleshooting
| Problem | Solution |
|---|---|
ModuleNotFoundError: No module named 'matplotlib' |
Run pip install matplotlib again. Make sure you're using the same Python that you run scripts with. |
python: command not found |
Try python3 instead, or install Python. |
mpremote: command not found |
Run pip install mpremote. |
mpremote cp gives error |
Make sure the Pico is connected via USB and no REPL session is open. |
| Plot window doesn't appear | On some systems (WSL, SSH), try adding plt.savefig("plot.png") before plt.show() and open the PNG manually. |
| CSV file is empty or garbled | Check that the robot code finished cleanly (reached f.close()). If you hit Ctrl+C during logging, the last line may be incomplete — delete it from the CSV. |
No matplotlib? Alternatives
If you can't install matplotlib:
- Google Sheets: Upload the CSV, select columns, Insert → Chart
- Excel / LibreOffice Calc: Open the CSV, select data, Insert → Line Chart
- Online: Paste CSV data into csvplot.com
These work for quick visual checks. For systematic tuning (overlaying multiple runs, annotating), matplotlib is worth the setup.