Interlaced Roots: Strum’s Separation Theorem

Strum’s separation theorem says that the zeros of independent solutions to an equation of the form

alternate. That is, between any two consecutive zeros of one solution, there is exactly one zero of the other solution. This is an important theorem because a lot of differential equations of this form come up in applications.

Original Link

Histograms With Python

In this article, we will look into the details of drawing a histogram using the matplotlib library. The importance of a histogram is how information is grouped together so that it can be compared and analyzed. This article provides the nitty-gritty of drawing a histogram using the matplotlib library.

A histogram is a powerful technique in data visualization. Drawing a histogram in Python is relatively easy. All you have to do is code for 3-4 lines of code. That looks pretty straightforward. But complexity is involved when we deal with live data for visualization. 

In order to draw a histogram, we need to know the following concepts clearly. They are as follows:

  • Axis: y-axis and x-axis.

  • Data: The data can be represented as an array.

  • Height and width of bars. This is determined based on the analysis. The width of the bar is called bin or intervals.

  • Title of the histogram.

  • Color of the bar.

  • Border color of the bar.

Based on the above information, we can draw a histogram using the following code.  

import numpy as np
import matplotlib.pyplot as plt
data = [1,11,21,31,41]plt.hist([1,11,21,31,41, 51], bins=[0,10,20,30,40,50, 60], weights=[10,1,40,33,6,8], edgecolor="red") 

This will draw a histogram, as shown below.

Image title

The above dataset is uniformly distributed in this tutorial. The interval is considered as 10 in this example, which means that the x-axis is marked a width of 10 units. In the given data, the width is distributed so that each bar takes a width of 10. This means that we have grouped the data based on the bin size. Here, the size of the bin is taken as 10. However, you can change the size of this interval based on the requirements.

The histogram is drawn from the smaller values to the highest on the x- and y-axis. In this case, the smaller value of x is 1 and highest value is 51 for y.

The arguments to plot in the histogram are dataset, bin, weights, face color, and edge color.

The weight in the above arguement represents the y-axis values. Basically, the dataset, bin, and weight attributes contribute in drawing a histogram. We can calculate the value of the bin dynamically, as well. The following code snippet does this job for you:

bins = range(min(data), max(data) + interval, interval)

interval is the width that is marked on the x-axis.

In the above graph, the default color is taken as blue while drawing. In order to change the color of a histogram, we can use face color attribute in the method arguments list. The above method can be defined as below:

plt.hist([1,11,21,31,41, 51], bins=[0,10,20,30,40,50, 60], weights=[10,1,0,33,6,8], facecolor='y', edgecolor="red")

It will display a histogram with a yellow color.

The edge color attribute will draw a border around the bar using the color mentioned. In this case, it is r for red.  

There are so many attributes mentioned in the matplotlib API for your perusal. 

Finally, we need to give a name to the histogram by calling plt.title("Histogram for 2018").

The histogram can be saved by clicking on the Save button on the GUI. Also, the following code will save the histogram as a PNG image.


It is important to understand the order of the commands. The savefig() method needs to be called before the show() method or else it will not save the current drawing.

You can go through the matplotlib API for more data visualization support.

Original Link

Python 3: Create Sparklines Using matplotlib

I recently wanted to create sparklines to show how some values were changing over time. In addition, I wanted to generate them as images on the server rather than introducing a JavaScript library.

Chris Seymour’s excellent gist, which shows how to create sparklines inside a Pandas DataFrame, got me most of the way there. But I had to tweak his code a bit to get it to play nicely with Python 3.6.

This is what I ended up with:

import matplotlib
import matplotlib.pyplot as plt
import base64 from io import BytesIO def sparkline(data, figsize=(4, 0.25), **kwags): """ Returns a HTML image tag containing a base64 encoded sparkline style plot """ data = list(data) fig, ax = plt.subplots(1, 1, figsize=figsize, **kwags) ax.plot(data) for k,v in ax.spines.items(): v.set_visible(False) ax.set_xticks([]) ax.set_yticks([]) plt.plot(len(data) - 1, data[len(data) - 1], 'r.') ax.fill_between(range(len(data)), data, len(data)*[min(data)], alpha=0.1) img = BytesIO() plt.savefig(img, transparent=True, bbox_inches='tight') plt.close() return base64.b64encode("UTF-8")

I had to change the class used to write the image from StringIO to BytesIO, and I found I needed to decode the bytes produced if I wanted it to display in an HTML page.

This is how you would call the above function:

if __name__ == "__main__": values = [ [1,2,3,4,5,6,7,8,9,10], [7,10,12,18,2,8,10,6,7,12], [10,9,8,7,6,5,4,3,2,1] ] with open("/tmp/foo.html", "w") as file: for value in values: file.write('<div><img src="data:image/png;base64,{}"/></div>'.format(sparkline(value)))

And the HTML page looks like this:

2017 09 23 07 49 32

That was easy!

Original Link