plotext plots data directly on terminal
Project description
plotext
plots directly on terminal, it has no dipendencies the syntax is very similar to matplotlib
.
Scatter Plot
Here is a basic example of a scatter plot:
import plotext as plt
y = [1, 5, 3, 8, 4, 9, 0, 5]
plt.scatter(y)
plt.show()
which prints this on terminal:
Note that you could also pass both the x
and y
coordintates to the scatter
function using plt.scatter(x, y)
.
Line Plot
For a line plot use the the plot
function instead:
import plotext as plt
y = [1, 5, 3, 8, 4, 9, 0, 5]
plt.plot(y)
plt.show()
Note that you could also pass both the x
and y
coordintates to the plot
function using plt.plot(x, y)
.
Multiple Data
Multiple data sets can be plotted using consecutive scatter
or plot
functions. Here is a basic example:
import plotext as plt
y = [1, 5, 3, 8, 4, 9, 0, 5]
plt.plot(y, label = "lines")
plt.scatter(y, label = "points")
plt.show()
- Using the
label
parameter inside the plotting calls, a legend is automatically added in the upper left corner of the plot. - The function
plt.legend()
provides an alternative way to set all the plot labels. Here is an equivalent version of the previous example:
import plotext as plt
y = [1, 5, 3, 8, 4, 9, 0, 5]
plt.plot(y)
plt.scatter(y)
plt.legend(["lines", "points"])
plt.show()
Plot Limits
The plot limits are set automatically, to set them manually you can use the following functions - to be placed after the plotting calls and before show()
:
plt.xlim(xmin, xmax)
sets the minimum and maximum limits of the plot on thex
axis. It requires a list of two numbers, where the firstxmin
sets the left (minimum) limit and the secondxmax
the right (maximum) limit. If one or both values are not provided, they are calculated automatically.plt.ylim(ymin, ymax)
is the equivalent ofplt.xlim()
but for they
axis.
Here is a coded example:
import plotext as plt
import numpy as np
l = 1000
x = np.arange(l)
n = 2
f = n * np.pi / l
y = np.sin(n * f * x)
plt.scatter(x, y)
plt.xlim(x[0] - 100, x[-1] + 100)
plt.ylim(-1.2, 1.2)
plt.show()
Data Ticks
You can change the numerical ticks on both axes with the following three functions - to be placed after the plotting calls and before show()
:
plt.ticks(xnum, ynum)
setsxnum
number of ticks on thex
axis andynum
number of ticks on they
axis respectivelly.plt.xticks(ticks, labels)
manually sets thex
ticks to the list oflabels
at the list of coordinates provided inticks
. If only one list is provided (ticks
), the labels will correspond to the coordinates.plt.yticks(ticks, labels)
is the equivalent ofplt.xticks()
but for they
axis.
Here is a coded example:
import plotext as plt
import numpy as np
l = 1000
x = np.arange(l)
n = 2
f = n * np.pi / l
y1 = np.sin(n * f * x)
y2 = y1 * np.exp(-0.25 * f * x)
xticks = np.arange(0, l + l / (2 * n), l / (2 * n))
xlabels = [str(i) + "π" for i in range(2 * n + 1)
plt.scatter(x, y1, label = "periodic signal")
plt.scatter(x, y2, label = "decaying signal")
plt.ticks(0, 7)
plt.xticks(xticks, xlabels)
plt.show()
Plot Aspect
You can personalize the plot aspect in many ways. You could use the following parameters - to be placed inside the scatter
or plot
calls:
point_marker = marker
sets the marker used to indentify each data point to the specified character. For exampleplt.scatter(data, point_marker = "x")
. An integer value (up to 9) can also be provided to access special characters.line_marker = marker
sets the marker, used to indentify the lines between consecutive points, to the specified character. For exampleplt.plot(data, line_marker = "x")
. An integer value (up to 9) can also be provided to access special characters.point_color = color
sets the color ofpoint_marker
on the plot.line_color = color
sets the color ofline_marker
on the plot.fill = True
fills the area between the data and they = 0
level with data points (if used insidescatter
) or line points (if used insideplot
). For example:plt.plot(data, fill = True)
. By defaultfill = False
You could also use the following functions - to be placed after the plotting calls and before show()
:
plt.figsize(width, height)
sets the width and height of the plot to the desired values in terms of number of characters and characters rows on terminal. Note that the plot automatically extends to fill the entire terminal: use this function in order to reduce this size. Note also that the plot dimensions have a minimum value, dependent on the presence of axes, ticks, title etc; the plot dimensions will be set to the minimum value if a smaller size is provided.plt.width(width)
changes the figure width alone.plt.height(height)
changes the figure height alone.plt.title(string)
adds a plot title on the top of the plot.plt.xlabel(string)
andplt.ylabel(string)
adds a label for respectively thex
andy
axis on the bottom of the plot.plt.grid(xbool, ybool)
adds thex
grid lines to the plot ifxbool == True
and they
grid lines ifybool == True
. If only one boolean value is provided both girdlines are set simultaneously.plt.axes(xbool, ybool)
adds thex
axis ifxbool == True
and they
axis ifybool == True
. If only one boolean value is provided both axes are set simultaneously.plt.frame(True)
adds a frame around the figure. Note thatplt.frame(False)
will remove the frame only if the primaryx
ory
axis are absent, otherwise only the seconday axes are removed.plt.canvas_color(color)
sets the color of the plot canvas alone.plt.axes_color(color)
sets the background color of all the labels sourrounding the actual plot, i.e. the axes, ticks, title and axes labels, if present.plt.ticks_color(color)
sets the (fullground) color of the axes ticks and of the gridlines, if present.plt.nocolor()
removes all colors from the plot.
Other functions:
plt.terminal_size()
returns the current terminal size.plt.colors()
prints the available fullground and background color codes. Here is the output for simplicity:
Fullground colors can be set to point_color
and line_color
or given as input to plt.ticks_color()
. Background colors can be given as input to plt.canvas_color()
and plt.axes_color()
.
Using flash
will result in an actually flashing character.
-
plt.markers()
shows the optional integer codes to quickly access special point or line markers. Here is the output for simplicity:
which can be set to point_marker
and line_marker
.
Here is a coded example:
import plotext as plt
import numpy as np
l = 1000
x = np.arange(l) + 1
n = 2
f = n * np.pi / l
y1 = np.sin(n * f * x)
y2 = y1 * np.exp(-0.25 * f * x)
plt.plot(x, y1, label = "periodic signal", line_color = "tomato")
plt.scatter(x, y2, label = "decaying signal", point_color = "iron", fill = True)
plt.grid(True)
plt.title("plotext - plot style")
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.canvas_color("cloud")
plt.axes_color("blue")
plt.ticks_color("yellow")
plt.show()
Streaming Data
When streaming a continuos flow of data, consider using the following functions - to be placed before the plotting calls:
plt.clear_plot()
clears the plot and all its internal parameters; it is useful when running the same script several times in order to avoid addind the same data to the plot; it is very similar tocla()
inmatplotlib
.plt.clp()
is the shorter but equivalent version ofplt.clear_plot()
.plt.clear_terminal()
clear the terminal before the actual plot.plt.clt()
is the shorter but equivalent version ofplt.clear_terminal()
.plt.sleep(time)
is used in order to reduce a possible screen flickering; for exampleplt.sleep(0.01)
would add approximately 10 ms to the computation. Note that thetime
parameters will depend on your processor speed and it needs some manual tweaking. You can place this command also after the plotting calls andshow()
function.
Here is a coded example:
import plotext as plt
import numpy as np
l = 1000
n = 2
f = n * np.pi / l
x = np.arange(0, l)
xticks = np.linspace(0, l-1, 5)
xlabels = [str(i) + "π" for i in range(5)]
frames = 500
for i in range(frames):
y = np.sin(n * f * x + 2 * np.pi / frames * i)
plt.clp()
plt.clt()
plt.scatter(x, y)
plt.ylim(-1, 1)
plt.xticks(xticks, xlabels)
plt.yticks([-1, 0, 1])
plt.fig_size(150, 40)
plt.title("plotext - streaming data")
plt.nocolor()
plt.sleep(0.001)
plt.show()
- The function
plt.nocolor()
is reccomended to make the streaming more responsive. - Plotting the same data using
matplotlib
was roughly 10 to 50 times slower on my Linux-based machine (depending on the colors settings and data size).
Other Functions
plt.savefig(path)
saves the plot as a text file at thepath
provided. Note: no colors are preserved at the moment, when saving.plt.version()
returns the version of the current installedplotext
package.plt.parameters()
returns all the internal plot parameters.plt.docstrings()
prints all the available docstrings.
Installation
Use pip install plotext --upgrade
Main Updates:
- from version 2.2.1: new project descritpion file,
fig_size
becomesfigsize
andfacecolor
is back toaxes_color
(sorry for confusion). - from version 2.2.0: new markers that are Windows friendly (when the plot is saved, they occupy one character)
- the plots are printed with a default color combination, instead of default colorless one.
- the
x
axis is now on the left side force_size
parameter removed (please let me know if it is still needed).grid
function added to add optional grid lines.frame
function added to add a frame (present by default).- the only parameters available in the
plot
andscatter
function are now only those which are dependent on the data set (likepoint_marker
,point_color
,fill
etc..), all others can be set before theshow()
function with dedicated functions (liketicks()
,title()
etc.. ) fig_size()
instead ofcanvas_size()
to avoid confusionnocolor()
function added- better algorith for getting the lines between consecutive points and the filling point (when using
fill=True
). clp()
andclt()
functions created, short versions ofclear_plot()
andclear_terminal()
respectively.- color codes updated.
parameters()
funtion created.docstrings()
function created.
Future Plans:
- creation of an histogram plot.
- creation of bar plots.
- creation of logarithmic plots
- creation of subplots
- data ticks for time based data
- color and terminal size support for IDLE python editor and compiler.
- same as previous point but for Spider.
- saving text files with color
Any help or new ideas are welcomed.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file plotext-2.2.2.tar.gz
.
File metadata
- Download URL: plotext-2.2.2.tar.gz
- Upload date:
- Size: 18.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/51.0.0 requests-toolbelt/0.8.0 tqdm/4.51.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5257c87d0b04d554f153576ff6c365719d55de56b013a01da0fec3e05ab9360c |
|
MD5 | f84de6e9d6a7787dfed2a69df0f6b6d4 |
|
BLAKE2b-256 | 94172a73903126c75d73b8ed12bfaa47f09d03a98039ea3b5d9546fc1e71d437 |
File details
Details for the file plotext-2.2.2-py3-none-any.whl
.
File metadata
- Download URL: plotext-2.2.2-py3-none-any.whl
- Upload date:
- Size: 14.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/51.0.0 requests-toolbelt/0.8.0 tqdm/4.51.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34fe3e45f5e602169aa068c4e827527aac75ec7263b852ab2fd88686df7efb04 |
|
MD5 | a785052d0d8183a470f29efa07d1c789 |
|
BLAKE2b-256 | 6a2ccc2e29f8574591364e461a4bc9cecf1c61f0926778805c2dbae19ac90e85 |