Outils pour utilisateurs

Outils du site


issue187:python

Ceci est une ancienne révision du document !


Back in Full Circle # 171 (July 2021), we looked at Plotext, a Python package that plots directly to a terminal. Over the past 16 months, much has changed in Plotext. It has gone from version 4.1.1 to 5.2.8, and while there are many internal code changes, there is also some new functionality. Their repository page is located at https://github.com/piccolomo/plotext.

Of course, as always, you need to install and/or upgrade the package using pip.

pip3 install –upgrade plotext

Once you have done this, you can start trying it out using Python in a terminal.

If, on the other hand, you want to write the program, it would look something like that shown top right.

Besides the scatter plot, there are line plots, log plots, stem plots, plots with multiple data sets and multiple axes, vertical bar plots, horizontal bar plots, multiple bar plots, stacked bar plots, histogram plots, datetime plots and candlestick plots – all without the need of using matplotlib.

When I originally found Plotext, I was interested in using the package to display plots within a Tkinter program using a standard widget like a Tk Label widget. It took a while, but digging through the online documentation and the source code, I was able to find a way by saving the plot to a file, then reading it in as text into the label object. The biggest problem that I had was that the size of the plot was based strictly on the size of the terminal when you ran the plot. Things didn’t always line up, especially when you were running the program within an IDE.

I contacted the author, and happily, he was able to provide a fix which ended up in version 4.1.1.

So I was very interested in the new version. When I tried to run the new version, there was an issue that caused the plot to be displayed as a mess.

I contacted the author again, this time on a Sunday morning, and within 30 minutes, I was provided a work-around that required only a one-line change. There were a few small things that also needed to be rearranged a bit, but in the end, the process was very similar to the 4.1.1 version.

Take, for example, the code to produce a simple sine wave and display it in a Tk Label widget.

  Shown top right, line 1 is the plotext command to create the plot with whichever marker the user has selected. Next, we tell the library not to limit the plot to the size of the terminal. There are two boolean values, one for the x dimensions and one for the y. We then tell the library the size we want the plot to be, and then clear the color data (plt.clc()). Finally, we get to the workaround – which is to build the plot in the uncolorized mode – and assign it to an object which is finally sent to the Label widget via the textvar parameter.

Of course, some of the plots are more complicated, so the first line becomes multiple lines, but the last five lines are the same for every type of standard plot.

There are two new plot types available or greatly upgraded since the first version that I tried, image plots and video plots. Image plots take an image in .jpg format (and I believe others as well), and display it directly into a terminal window. Their sample code is shown bottom right.

Which downloads, displays then deletes a .jpg image in the terminal.

Which admittedly, doesn’t do a great job with the default settings, but when set properly, will give you something like this.

You can also use it (with a few changes to the code) to view animated GIF images and to view youtube videos or .mp4 videos.

While I’m not really sure why one would want to do these things, it does show the power of the Plotext library.

The bottom line is that Plotext is a great add-on library for Python, and if you need to do light-weight plotting either in a terminal or to a Tkinter GUI, you can’t do better!

Until next time, as always; stay safe, healthy, positive and creative!

issue187/python.1669534840.txt.gz · Dernière modification : 2022/11/27 08:40 de auntiee