Outils pour utilisateurs

Outils du site


issue171:python

Ceci est une ancienne révision du document !


In the poem “to a mouse”, Robert Burns wrote “The best laid schemes o’ Mice an’ Men Gang aft agley”. Now, some of us might not get Scottish quite readily, so with a little bit of web searching, the general gist of the saying is (and this is the opinion of multiple website writers) “the best laid plans of mice and men can still go wrong. No matter how well you plan, there is no guarantee of success.” That seems to be speaking directly to my life.

The article you are reading was not my first choice of subjects for this month. In fact, it didn’t make it into the top three subjects. But such is life. When life gives you lemons, what do you do? You make Lemon Drop Martinis. That’s what.

Plotext

I’m always looking for a way to produce plots that I can use easily, quickly and differently. I found what I think is a really good one. ‘Plotext’ plots directly on terminal; it has no dependencies; and the syntax is very similar to matplotlib. It also provides a simple command-line tool.

I haven’t had a tremendous amount of time to fully test and examine plotext, but by the time you get to the end of this article, you will see that it isn’t very difficult to use.

You can see the project at https://github.com/piccolomo/plotext .

It’s easy to install using pip (or pip3).

pip install plotext

Once I had the library loaded, I pulled open a ptpython REPL. I borrowed one of the many examples from the repository (shown right).

Working with real data

I wanted to see just how easy it is using some real data. I did a quick web search for the latest Covid-19 data for confirmed cases in Texas from March 3, 2020 until July 12, 2021. It was a very large amount of data, so I simply pulled up the data in a spreadsheet and pulled the totals for all of Texas for the last 40 days. Then I threw together the following program (next page, top right).

You can see that the ‘last 40 days’ shows what the news is reporting. Despite the attempts to control Covid-19, the number of confirmed cases rose at a fairly steep rate.

Next, I tried to pull the data just for one county in Texas (bottom right). Harris county is where most of Houston is located. Houston has over 4 million residents and is the fourth largest city in the United States.

This time I pulled only 28 days worth of data. This plot shows that, in Harris county, things were starting to stabilize and actually going down until around the 28th of June, then started going up at a fairly quick rate.

Finally, I pulled the data for Hays County in Texas (next page, top right). Hays county is a fairly small county in terms of population, only around 157,000. Many of the residents of Hays county are college students.

Again, the number of confirmed cases went from 17150 to 19230 in a period of 103 days. While that’s only 2088 more cases over the sample period, I find it interesting that the number of cases were rising at a fairly steady rate until about the time that the students started heading home for the summer break. Then the number of cases still went up but at a much slower rate.

As you can see, with a very little amount of effort, you can get reasonable plots. According to the repository site, you can do scatter plots, line plots, log plots, stem plots, multiple data sets, double Y axis plots, bar plots, histogram plots, set the data ticks to custom values, date/time plots, multiple subplots, plot streaming data, and more.

It looks like the developers are determined to continue updating the package, since everything on the site seems to have been updated within the last couple of months. They are currently on version 3.13.

This is a great little package and I intend to learn more about it and update you as I can.

The code that I used to produce the charts can be found at https://github.com/gregwa1953/FCM-171 .

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

issue171/python.1627838266.txt.gz · Dernière modification : 2021/08/01 19:17 de auntiee