Python package simplifies algebraic equations
Beautiful and Colorful
To demonstrate how the two faucets interact while filling the tub, the three bar graphs in Figure 4 represent the respective fill levels by minute. In the graph on the left, the bath user only turns on the slow faucet, which fills the tub in 15 minutes; in the middle, the 10-minute fast faucet is active; and, on the right, both faucets are active together.
![](/var/linux_magazin/storage/images/issues/2021/248/programming-snapshot-sympy/figure-4/787292-1-eng-US/Figure-4_large.png)
In Listing 4, starting in line 5, the fill_tub()
function takes the tub fill rate per minute (per_min
) as well as the number of values on the X axis displayed (i.e., the minute ticker). Although the scenarios presented each cover different sets of minute values, the matplotlib
functions insist that X and Y values be arrays of equal size, or else they will provoke non-intuitive error messages from the depths of the library. The fill_tub()
function returns two arrays lx
and ly
that contain values for the X and Y axes in the bar graph. The two arrays are the same length, and unoccupied Y values with the tub already full are simply set to zero for initialization purposes in line 7.
Listing 4
bars.py
01 #!/usr/bin/env python3 02 from matplotlib import pyplot as plt 03 from fractions import Fraction 04 05 def fill_tub(per_min,xmax): 06 lx=list(range(1,xmax+1)) 07 ly=[0] * xmax 08 sum=0 09 for i in range(xmax): 10 sum+=per_min 11 if sum > 1: 12 break 13 ly[i]=sum 14 15 return lx,ly 16 17 xmax=15 18 19 plt.style.use('ggplot') 20 fig,ax = plt.subplots(nrows=1,ncols=3,figsize=(15,5)) 21 fig.suptitle("Filling a bath tub") 22 23 lx,ly = fill_tub(Fraction(1,15),xmax) 24 ax[0].bar(lx,ly,color='tab:red') 25 ax[0].set_xlabel("Minutes") 26 ax[0].set_ylabel("Fill Level") 27 ax[0].set_title("15min faucet") 28 29 lx,ly = fill_tub(Fraction(1,10),xmax) 30 ax[1].bar(lx,ly,color='tab:orange') 31 ax[1].set_xlabel("Minutes") 32 ax[1].set_ylabel("Fill Level") 33 ax[1].set_title("10min faucet") 34 35 lx,ly = fill_tub(Fraction(1,10)+Fraction(1/15),xmax) 36 ax[2].bar(lx, ly, color='tab:green') 37 ax[2].set_xlabel("Minutes") 38 ax[2].set_ylabel("Fill Level") 39 ax[2].set_title("Both 15min and 10min faucet") 40 41 plt.savefig("bars.png")
Painting three different graphs side by side in an image file is a piece of cake for matplotlib
: The subplots()
function simply creates a graph grid with one row as well as three columns in line 20 and returns an array of three graph objects in ax
. With these settings, matplotlib
manages to arrange the three graphs beautifully, without any manual intervention. Finally, the last line writes the image data to a newly created image file bars.png
.
Exact Fraction Calculation
To prevent the program from immediately turning a fraction like 1/15 into a floating-point number, Listing 4 pulls in the fractions package, which handles real fractional arithmetic. Thus, line 23 with Fraction(1, 15)
can actually pass 1/15 to fill_tub()
rather than, say, 0.066666666667
. The latter would cause nasty rounding errors when summing up fractional values later and possibly even cause the tub to overflow. The fractions package, on the other hand, overloads operators like +
or >
. Therefore, sum += per_min
in line 10 actually adds 1/15 to the amount of water in the tub, and sum
inside the if
condition in line 11 is later exactly 1
when the tub is full, rather than some close, but not quite right floating-point value.
Infos
- Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/linux-magazine.com/248/
- Virtual environments: https://docs.python.org/3/tutorial/venv.html
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.