Matplotlib Online Compiler

Write, Run & Share Matplotlib code online using OneCompiler's Matplotlib online compiler for free. It's one of the robust, feature-rich online compilers for the Matplotlib library, running on Python 3.12 with NumPy pre-installed. Plot windows are streamed live into the browser — plt.show() opens a real Tk window inside the run sandbox and you see it in the output panel. The editor shows sample boilerplate code when you choose language as Matplotlib and start coding.

About Matplotlib

Matplotlib is the foundational plotting library for Python — first released by John Hunter in 2003 and now the engine behind most scientific charts, papers, and notebooks you see in the wild. Almost every other Python visualization tool (Seaborn, Pandas .plot(), scikit-learn's plotting helpers) is built on top of it. The pyplot interface is what most people use day-to-day: a stateful, MATLAB-style API that turns a few lines of NumPy into a publication-quality figure.

Adding dependencies

NumPy and Matplotlib are pre-installed. To add anything else (pandas, scipy, scikit-learn…), open requirements.txt in the file tree and add one package per line — the first run installs them, subsequent runs are cached.

matplotlib
numpy
pandas
scipy

Syntax help

A first plot

plt.plot() draws a line, plt.show() opens the window. Use matplotlib.use("TkAgg") so the figure renders into the GUI sandbox.

import numpy as np
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 200)
plt.plot(x, np.sin(x))
plt.title("sin(x)")
plt.xlabel("x")
plt.ylabel("sin(x)")
plt.grid(True)
plt.show()

Multiple series, legend, styling

Call plot() more than once before show() to layer series on the same axes. Each call accepts a format string ("r--", "go-", "b:") and keyword arguments like linewidth, marker, and label.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)

plt.plot(x, np.sin(x), label="sin(x)", linewidth=2)
plt.plot(x, np.cos(x), "r--", label="cos(x)")
plt.plot(x, np.sin(x) * np.cos(x), "g:", label="sin·cos", linewidth=1.5)

plt.title("Trig functions")
plt.legend(loc="upper right")
plt.grid(alpha=0.3)
plt.show()

Common chart types

pyplot exposes a dedicated function per chart type. Same data, very different stories.

import numpy as np
import matplotlib.pyplot as plt

# Bar chart
plt.figure()
plt.bar(["Python", "Go", "Rust", "Java"], [120, 75, 60, 95], color="steelblue")
plt.title("Stars by language")
plt.show()

# Histogram
plt.figure()
plt.hist(np.random.normal(0, 1, 1000), bins=30, color="teal", edgecolor="white")
plt.title("Normal distribution")
plt.show()

# Scatter plot
plt.figure()
x = np.random.rand(100)
y = x + np.random.normal(0, 0.1, 100)
plt.scatter(x, y, alpha=0.6, c="purple")
plt.title("Scatter")
plt.show()

# Pie chart
plt.figure()
plt.pie([35, 25, 20, 20], labels=["A", "B", "C", "D"], autopct="%1.1f%%")
plt.title("Share")
plt.show()

Subplots

plt.subplots(rows, cols) returns a Figure and an array of Axes. Each Axes has its own plot, set_title, set_xlabel, etc.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 200)

fig, axes = plt.subplots(2, 2, figsize=(8, 6))

axes[0, 0].plot(x, np.sin(x))
axes[0, 0].set_title("sin")

axes[0, 1].plot(x, np.cos(x), color="orange")
axes[0, 1].set_title("cos")

axes[1, 0].plot(x, np.tan(x), color="red")
axes[1, 0].set_ylim(-5, 5)
axes[1, 0].set_title("tan (clipped)")

axes[1, 1].hist(np.random.normal(size=500), bins=20, color="green")
axes[1, 1].set_title("histogram")

fig.tight_layout()
plt.show()

Object-oriented API

For anything more than a quick chart, prefer the explicit Figure/Axes API — it scales better when you have multiple plots or want to pass an axes around to a helper.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 200)

fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(x, np.sin(x), label="sin(x)")
ax.fill_between(x, np.sin(x), alpha=0.2)

ax.set_title("Object-oriented Matplotlib")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_xlim(0, 10)
ax.set_ylim(-1.2, 1.2)
ax.legend()
ax.grid(True, linestyle="--", alpha=0.5)

plt.show()

Styles and themes

plt.style.use() flips every chart to a preset look. print(plt.style.available) lists them.

import matplotlib.pyplot as plt

plt.style.use("ggplot")          # try: 'seaborn-v0_8', 'dark_background', 'bmh', 'fivethirtyeight'

plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.title("Styled chart")
plt.show()

Saving figures

plt.savefig() writes the current figure to disk before (or instead of) show(). Format is inferred from the extension — png, pdf, svg, jpg.

import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [1, 4, 9])
plt.title("My chart")
plt.savefig("chart.png", dpi=150, bbox_inches="tight")
plt.show()

Annotations and text

Add arrows, labels, and free text to highlight features in your data.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y)
plt.annotate("local max",
             xy=(np.pi / 2, 1),
             xytext=(3, 1.3),
             arrowprops=dict(arrowstyle="->", color="red"))
plt.text(7, -0.8, "f(x) = sin(x)", fontsize=12, color="gray")
plt.ylim(-1.5, 1.5)
plt.show()