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.10+.

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")

fig, ax = plt.subplots()

fruits = ['apple', 'blueberry', 'cherry', 'orange']
counts = [40, 100, 30, 55]
bar_labels = ['red', 'blue', '_red', 'orange']
bar_colors = ['tab:red', 'tab:blue', 'tab:red', 'tab:orange']

ax.bar(fruits, counts, label=bar_labels, color=bar_colors)

ax.set_ylabel('fruit supply')
ax.set_title('Fruit supply by kind and color')
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 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 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.3.0.tar.gz (19.3 kB view hashes)

Uploaded Source

Built Distribution

mpl_ascii-0.3.0-py3-none-any.whl (10.1 kB view hashes)

Uploaded Python 3

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