Python ASCII plots in terminal
Categories:
Crafting ASCII Plots in Your Python Terminal

Learn how to generate simple yet effective ASCII-based plots directly within your terminal using Python, perfect for quick data visualization without a GUI.
Visualizing data is crucial for understanding trends and patterns. While powerful libraries like Matplotlib offer rich graphical plots, sometimes you need a quick, lightweight visualization directly in your terminal. This is where ASCII plots shine. They are text-based representations of data that can be generated and viewed in any terminal environment, making them ideal for remote servers, command-line tools, or simply for rapid prototyping without the overhead of a graphical user interface.
Why ASCII Plots?
ASCII plots provide several advantages, especially in specific development contexts. They are incredibly portable, requiring no special graphical libraries or display servers, which is a huge plus when working on headless systems or over SSH. They are also fast to generate and consume minimal resources. While they lack the aesthetic appeal and interactivity of GUI-based plots, their simplicity makes them excellent for debugging, quick checks, and integrating into command-line utilities. This article will guide you through creating basic ASCII plots using Python, focusing on common libraries that simplify the process.
flowchart TD A[Start: Data Available] --> B{Need Quick Terminal Viz?} B -- Yes --> C[Choose ASCII Plotting Library] C --> D[Prepare Data for Plotting] D --> E[Generate ASCII Plot] E --> F[Display in Terminal] B -- No --> G[Use GUI Plotting Library (e.g., Matplotlib)] G --> H[Generate Graphical Plot] H --> I[Display in GUI Window] F --> J[End] I --> J[End]
Decision flow for choosing between ASCII and GUI plotting.
Getting Started with plotille
plotille
is a popular Python library specifically designed for creating ASCII plots. It supports various plot types, including scatter plots, line plots, and histograms, and offers good customization options for terminal output. To begin, you'll need to install it.
pip install plotille
Install the plotille
library.
Once installed, you can start generating plots. Let's create a simple line plot to visualize a sine wave.
import numpy as np
import plotille
# Generate some data
x = np.linspace(0, 2 * np.pi, 50)
y = np.sin(x)
# Create a line plot
fig = plotille.plot(x, y, width=60, height=20, X_label='Angle (rad)', Y_label='Sine Value', title='Sine Wave ASCII Plot')
print(fig)
Python code to generate an ASCII line plot of a sine wave using plotille
.
width
and height
parameters in plotille.plot()
control the dimensions of your ASCII plot in characters. Adjust these to fit your terminal window for optimal viewing.Creating Bar Charts and Histograms
Beyond line plots, plotille
also excels at creating bar charts and histograms, which are essential for visualizing categorical data or distributions. Let's demonstrate how to create a simple bar chart and a histogram.
import plotille
import random
# --- Bar Chart Example ---
categories = ['A', 'B', 'C', 'D', 'E']
values = [random.randint(10, 100) for _ in categories]
# plotille's bar chart expects a dictionary or two lists for x and y
bar_data = {cat: val for cat, val in zip(categories, values)}
print("\n--- Bar Chart ---")
fig_bar = plotille.bar(list(bar_data.keys()), list(bar_data.values()), width=60, height=15, X_label='Category', Y_label='Value', title='Random Bar Chart')
print(fig_bar)
# --- Histogram Example ---
data = [random.gauss(0, 1) for _ in range(1000)] # 1000 random numbers from a normal distribution
print("\n--- Histogram ---")
fig_hist = plotille.hist(data, bins=10, width=60, height=15, X_label='Value', Y_label='Frequency', title='Normal Distribution Histogram')
print(fig_hist)
Python code for generating an ASCII bar chart and histogram.
bins
parameter determines the number of intervals (bars) the data will be divided into. Experiment with this value to find the best representation of your data's distribution.Alternative: asciichart
While plotille
is comprehensive, another lightweight option for simple line charts is asciichart
. It's particularly good for quick, single-line data visualizations.
pip install asciichartpy
Install the asciichartpy
library.
from asciichartpy import plot
import math
series = [math.sin(i / 10.0) for i in range(0, 100)]
print(plot(series, {'height': 10}))
Generating a simple sine wave plot using asciichartpy
.