The world’s leading publication for data science, AI, and ML professionals.

5 Interesting Python Libraries

Have you ever think data visualisation can be in command-line?

Photo by JOSHUA COLEMAN on Unsplash
Photo by JOSHUA COLEMAN on Unsplash

As one of the most popular programming languages, Python has a huge number of excellent libraries that facilitate development such as Pandas, Numpy, Matplotlib, SciPy, etc.

However, in this article, I’m going to introduce to you some libraries that are more interesting rather than very useful. I believe these libraries can show another aspect of Python and the thrive of the community.

1. Bashplotlib

Photo by David Werbrouck on Unsplash
Photo by David Werbrouck on Unsplash

Honestly, when I first time see this library, I questioned why people may need this? Bashplotlib is a Python library that enables us to plot data in a command-line stdout environment.

Soon I realised that it is probably going to be useful when you don’t have any GUI available. Well, this scenario may not be that frequent, but it doesn’t prevent my curiosity and to feel it is a very interesting Python library.

Bashplotlib can be easily installed with pip.

pip install bashplotlib

Let’s see some examples. In the code below, I imported numpy to generate some random arrays, as well as the bashplotlib, of course.

import numpy as np
from bashplotlib.histogram import plot_hist
arr = np.random.normal(size=1000, loc=0, scale=1)

plot_hist is a function from bashplotlib that is for plotting 1-D data in a histogram, just like plt.hist does in Matplotlib. Then, I use Numpy to generate a random array with 1,000 numbers that are normally distributed. After that, we can easily plot this data as follows:

plot_hist(arr, bincount=50)

The output is like this

Isn’t that interesting? 🙂

Also, you can plot your data in a scatter plot from text files.

2. PrettyTable

Photo by Goran Ivos on Unsplash
Photo by Goran Ivos on Unsplash

The Bashplotlib I have just introduced is for plotting data in the command-line environment, whereas PrettyTable is for output tables in a pretty format.

Similarly, we can easily install this library using pip.

pip install prettytable

Firstly, let’s import the lib.

from prettytable import PrettyTable

Then, we can use PrettyTable to create a table object.

table = PrettyTable()

Once we have the table object, we can start to add fields and data rows.

table.field_names = ['Name', 'Age', 'City']
table.add_row(["Alice", 20, "Adelaide"])
table.add_row(["Bob", 20, "Brisbane"])
table.add_row(["Chris", 20, "Cairns"])
table.add_row(["David", 20, "Sydney"])
table.add_row(["Ella", 20, "Melbourne"])

To display the table, just simply print it!

print(table)

PrettyTable also supports refining the table styles, in almost every perspective that you may think. For example, you can right-align the text in the table:

table.align = 'r'
print(table)

Sort table by a column

table.sortby = "City"
print(table)

You can even get the HTML string of the table

3. FuzzyWuzzy

Photo by Brett Jordan on Unsplash
Photo by Brett Jordan on Unsplash

This library is not only very interesting but also very useful, in my opinion. Many times you may want to implement a "fuzzy" search feature for your program. FuzzyWuzzy provides you with an out-of-box and light-weighted solution for this.

Install it from pip as usual.

pip install fuzzywuzzy

Import the library:

from fuzzywuzzy import fuzz

Let’s do a simple test.

fuzz.ratio("Let's do a simple test", "Let us do a simple test")

As shown the result "93" means that these two strings have 93% similarity, which is pretty high.

When you have a list of strings, and you want to search a term against all of them, FuzzyWuzzy will help you to extract the most relevant ones with their similarities.

from fuzzywuzzy import process
choices = ["Data Visualisation", "Data Visualization", "Customised Behaviours", "Customized Behaviors"]
process.extract("data visulisation", choices, limit=2)
process.extract("custom behaviour", choices, limit=2)

In the above example, the parameter limit tells FuzzyWuzzy to extract the "top n" results for you. Otherwise, you will get a list of tuples with all of these original strings and their similarity scores.

4. TQDM

Photo by Jungwoo Hong on Unsplash
Photo by Jungwoo Hong on Unsplash

Do you usually develop command-line tools using Python? If so, this interesting library is going to help you when your CLI tool is processing something time-consuming by showing a progress bar to indicate how much has been done.

Installation using pip, again.

pip install tqdm

When you have a for-loop using range function, just replace it by trange from tqdm.

from tqdm import trange
for i in trange(100):
    sleep(0.01)

More generally, you may want to loop a list. That’s also easy with tqdm.

from tqdm import tqdm
for e in tqdm([1,2,3,4,5,6,7,8,9]):
    sleep(0.5)  # Suppose we are doing something with the elements

tqdm works for not only the command-line environment but also iPython/Jupyter Notebook.

image courtesy: https://github.com/tqdm/tqdm
image courtesy: https://github.com/tqdm/tqdm

5. Colorama

Photo by Greyson Joralemon on Unsplash
Photo by Greyson Joralemon on Unsplash

Do you want to add some colours to your command-line applications? Colorama makes it very easy to output everything in your preferred colour.

Installing Colorama needs pip again.

pip install colorama

Colorama supports to render output text colour in "foreground" (the text colour), "background" (the background colour) and "style" (extra styles of the colour). We can import

from colorama import Fore, Back, Style

Firstly, let’s show some warnings using yellow colour.

print(Fore.YELLOW)
print("This is a warning!")

Then, let’s try to show some errors using a red background colour.

print(Back.RED + Fore.WHITE + "This is an error!")

That red is too bright. Let’s use the "dim" style.

print(Back.RESET + Style.DIM + "Another error!")

Here we are setting "RESET" for back to change the background colour to default.

The "DIM" style makes the font kind of invisible. When we want to change everything back to normal, simply set "Style" to "RESET_ALL"

print(Style.RESET_ALL)

Summary

Photo by twinsfisch on Unsplash
Photo by twinsfisch on Unsplash

Thanks to these open-source developers who contribute to the Python community and thrive it.

Before I’ve seen the library Bashplotlib, I have to say that I never had such an idea to plot data in a command-line environment. No matter it may or may not be useful for you, I would say that the diversity of the development ideas and creatives of people never ended.

Join Medium with my referral link – Christopher Tao

If you feel my articles are helpful, please consider joining Medium Membership to support me and thousands of other writers! (Click the link above)


Related Articles