Skip to main content

A matplotlib backend that produces plots using only ASCII characters

Project description

mpl_ascii

A matplotlib backend that produces plots using only ASCII characters. It is available for python 3.7+.

Quick start

Install mpl_ascii using pip

pip install mpl_ascii

To use mpl_ascii, add to your python program

import matplotlib as mpl

mpl.use("module://mpl_ascii")

When you use plt.show() then it will print the plots as strings that consists of ASCII characters.

If you want to save a figure to a .txt file then just use figure.savefig("my_figure.txt")

See more information about using backends here: https://matplotlib.org/stable/users/explain/figure/backends.html

Examples

Bar chart

The following is taken from the example in examples/bar_color.py

import matplotlib.pyplot as plt
import matplotlib as mpl
import mpl_ascii

mpl_ascii.AXES_WIDTH=100
mpl_ascii.AXES_HEIGHT=40

mpl.use("module://mpl_ascii")

import matplotlib.pyplot as plt

# Example data
fruits = ['apple', 'blueberry', 'cherry', 'orange']
counts = [10, 15, 7, 5]
colors = ['red', 'blue', 'red', 'orange']  # Colors corresponding to each fruit

fig, ax = plt.subplots()

# Plot each bar individually
for fruit, count, color in zip(fruits, counts, colors):
    ax.bar(fruit, count, color=color, label=color)

# Display the legend
ax.legend(title='Fruit color')

plt.show()

bar chart with color

Scatter plot

The following is taken from the example in examples/scatter_multi_color.py

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import mpl_ascii

mpl_ascii.AXES_WIDTH=100
mpl_ascii.AXES_HEIGHT=40


mpl.use("module://mpl_ascii")

np.random.seed(0)
x = np.random.rand(40)
y = np.random.rand(40)
colors = np.random.choice(['red', 'green', 'blue', 'yellow'], size=40)
color_labels = ['Red', 'Green', 'Blue', 'Yellow']  # Labels corresponding to colors

# Create a scatter plot
fig, ax = plt.subplots()
for color, label in zip(['red', 'green', 'blue', 'yellow'], color_labels):
    # Plot each color as a separate scatter plot to enable legend tracking
    idx = np.where(colors == color)
    ax.scatter(x[idx], y[idx], color=color, label=label)

# Set title and labels
ax.set_title('Scatter Plot with 4 Different Colors')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')

# Add a legend
ax.legend(title='Point Colors')
plt.show()

Scatter plot with color

Line plot

The following is taken from the example in examples/double_plot.py

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import mpl_ascii

mpl_ascii.AXES_WIDTH=100
mpl_ascii.AXES_HEIGHT=40


mpl.use("module://mpl_ascii")


# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
c = 1 + np.cos(2 * np.pi * t)

fig, ax = plt.subplots()
ax.plot(t, s)
ax.plot(t, c)

ax.set(xlabel='time (s)', ylabel='voltage (mV)',
    title='About as simple as it gets, folks')

plt.show()

Double plot with colors

You can find more examples and their outputs in the examples folder.

Global Variables

mpl_ascii.AXES_WIDTH

Adjust the width of each axis according to the number of characters. The library first looks for the AXES_WIDTH as an environment variable. This can then be overwritten in the Python program by setting mpl_ascii.AXES_WIDTH. The final width of the image might extend a few characters beyond this, depending on the size of the ticks and axis labels. Default is 150.

mpl_ascii.AXES_HEIGHT

Adjust the height of each axis according to the number of characters. The library first looks for the AXES_HEIGHT as an environment variable. This can then be overwritten in the Python program by setting mpl_ascii.AXES_HEIGHT. The final height of the image might extend a few characters beyond this, depending on the size of the ticks and axis labels. Default is 40.

mpl_ascii.ENABLE_COLORS

Executing plt.show() will render the image in colored text. Default is True

Use cases

Using Version Control for Plots

Handling plots with version control can pose challenges, especially when dealing with binary files. Here are some issues you might encounter:

  • Binary Files: Committing binary files like PNGs can significantly increase your repository’s size. They are also difficult to compare (diff) and can lead to complex merge conflicts.

  • SVG Files: Although SVGs are more version control-friendly than binary formats, they can still cause problems:

    • Large or complex graphics can result in excessively large SVG files.
    • Diffs can be hard to interpret.

To mitigate these issues, ASCII plots serve as an effective alternative:

  • Size: ASCII representations are much smaller in size.
  • Version Control Compatibility: They are straightforward to diff and simplify resolving merge conflicts.

This package acts as a backend for Matplotlib, enabling you to continue creating plots in your usual formats (PNG, SVG) during development. When you’re ready to commit your plots to a repository, simply switch to the mpl_ascii backend to convert them into ASCII format.

Feedback

Please help make this package better by:

  • reporting bugs.
  • making feature requests. Matplotlib is an enormous library and this supports only a part of it. Let me know if there particular charts that you would like to be converted to ASCII
  • letting me know what you use this for.

If you want to tell me about any of the above just use the Issues tab for now.

Thanks for reading and I hope you will like these plots as much as I do :-)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mpl_ascii-0.10.0.tar.gz (49.1 kB view details)

Uploaded Source

Built Distribution

mpl_ascii-0.10.0-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file mpl_ascii-0.10.0.tar.gz.

File metadata

  • Download URL: mpl_ascii-0.10.0.tar.gz
  • Upload date:
  • Size: 49.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for mpl_ascii-0.10.0.tar.gz
Algorithm Hash digest
SHA256 8e4ae770d5a612dab0e8055c7677c6c3d271da4f5127cce46a60ce3ce4a4e72c
MD5 8e487378b3df79c63151de24d774dbd9
BLAKE2b-256 5383702b073e1d31cdb3fa292195d6fa21fa21b33f735adf0f130a5c0a8654d6

See more details on using hashes here.

File details

Details for the file mpl_ascii-0.10.0-py3-none-any.whl.

File metadata

  • Download URL: mpl_ascii-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for mpl_ascii-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8db7d7b29e9d92424008296c3a47334f48bc90068d468e27d800d376fc3af16a
MD5 b5b788a307ce4a829b93cfcae0a96bec
BLAKE2b-256 b2b83b363e49b7d3e70f2eb48dae10d0a4da0a9b46566897e1d27cbe0b5950f7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page