Python plotting for beginners

How to Plot a Graph in Python Online

Plot a graph in Python with Matplotlib in a few lines, right in your browser. Each example below opens in the online editor, where plt.show() displays the chart: change the numbers and run it again.

Plot points and join them with a line

plt.plot(x, y) draws a line through the points in order, and marker="o" puts a dot on each one. plt.title, plt.xlabel and plt.ylabel label the chart; nothing appears until plt.show().

first_graph.py
Run this graph →
first_graph.py
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5, 6]
y = [2, 4, 3, 6, 5, 8]

plt.plot(x, y, marker="o")
plt.title("My first graph")
plt.xlabel("x")
plt.ylabel("y")
plt.grid(True)
plt.show()

Plot a function such as sin(x) or x²

np.linspace makes 400 x values between two limits, and NumPy computes y for all of them at once. Plot several functions on the same axes and plt.legend() names them.

plot_function.py
Run this graph →
plot_function.py
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-2 * np.pi, 2 * np.pi, 400)

plt.plot(x, np.sin(x), label="sin(x)")
plt.plot(x, np.cos(x), label="cos(x)", linestyle="--")
plt.plot(x, 0.1 * x**2 - 1, label="0.1x² - 1")
plt.axhline(0, color="gray", linewidth=0.8)
plt.legend()
plt.title("Plot a function in Python")
plt.show()

Plot data from a CSV file

pandas reads the CSV (here from a string, or from a file uploaded to your workspace) and parses the dates. ax.twinx() adds a second y axis so temperature and rain share one chart.

plot_csv.py
Run this graph →
plot_csv.py
import matplotlib.pyplot as plt
import pandas as pd
from io import StringIO

csv_text = """date,temperature,rain_mm
2026-06-01,24,0
2026-06-02,26,2
2026-06-03,23,12
2026-06-04,21,8
2026-06-05,25,0
2026-06-06,28,0
2026-06-07,27,4"""

df = pd.read_csv(StringIO(csv_text), parse_dates=["date"])

fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(df["date"], df["temperature"], marker="o", color="tab:red", label="temperature (°C)")
ax2 = ax.twinx()
ax2.bar(df["date"], df["rain_mm"], alpha=0.3, label="rain (mm)")
ax.set_title("Weather for one week")
fig.autofmt_xdate()
plt.show()

Bar chart and pie chart

plt.subplots(1, 2) makes two charts side by side: bar for comparing amounts, pie for shares of a whole with percentages from autopct.

bar_and_pie.py
bar_and_pie.py
import matplotlib.pyplot as plt

fruits = ["apples", "bananas", "cherries", "dates"]
sold = [30, 45, 12, 20]

fig, (left, right) = plt.subplots(1, 2, figsize=(10, 4))
left.bar(fruits, sold, color=["#4c72b0", "#dd8452", "#c44e52", "#8172b3"])
left.set_title("Bar chart")
right.pie(sold, labels=fruits, autopct="%1.0f%%")
right.set_title("Pie chart")
plt.show()

Frequently asked questions

How do I plot a graph in Python?

Import matplotlib.pyplot as plt, call plt.plot(x, y) with your x and y values, add labels with plt.title, plt.xlabel and plt.ylabel, and call plt.show().

Can I plot a graph in Python online?

Yes. Open the online Matplotlib editor, paste your code and select Run. The chart appears in the page, with nothing to install.

How do I plot a math function?

Make x values with numpy.linspace, compute y with NumPy (for example np.sin(x) or x**2), then plt.plot(x, y).

How do I plot data from a CSV file?

Read it with pandas.read_csv, then plot the columns: plt.plot(df["date"], df["value"]). Upload the CSV to your workspace first.

Which chart should I use?

A line for trends over time, a bar chart to compare amounts, a scatter plot for the relation between two values, a histogram for a distribution and a pie chart for shares of a whole.