Plots With Python & Seaborn

Hi there. This page is a an overview of plots with the use of the Python programming language and its seaborn module. Plots include bar graphs, histograms, scatterplots, line graphs and math function plots.

To start out, import pandas, pyplot from matplotlib, seaborn and numpy into Python.

 

 

 


Sections

 


A Bar Graph Example

For the bar graph example, I create fake data for a survey on students’ favourite subject. I create a list of subjects and another list of counts. A dictionary is used into a pandas dataframe.

 

 

If you run print(subjects_df), you will notice that the columns are not in the desired order. The Count column should be the right column.

 

In order to rearrange the column order, a list of column titles is used along with the .reindex() method. The .sort_values() method is also used to sort the subjects from the highest counts to the smallest.

 

 

In creating the bar graph, I start with sns.set_style(“whitegrid”) from seaborn. Under the fig variable, there is seaborn’s barplot where x is with the Favourite subject and y is with the count. A title and labels are added to the plot.

 

 

Having horizontal bars instead of vertical bars is not too difficult with seaborn’s barplot. The simple modification is switching x and y around.

 

 

 

 


A Histogram Example

For the histogram example, I simulate 10000 standard normal random variables. This time around I use the darkgrid style which looks like R’s ggplot2 graphics. To achieve the histogram in seaborn, the distplot is needed.

 

 

 

 


Scatterplots

Generating scatterplots with seaborn is not too difficult. In this example, I create two lists of x and y values. These x and y lists are put into a pandas dataframe. Seaborn’s regplot will generate a scatterplot with fit_reg = False.

 

 

 

A Scatterplot With A Regression Line

Having fit_reg = True in Seaborn’s regplot will generate scatterplots with a linear regression line through the points. (A linear regression line from statistics is basically a line of best fit where the sum of the distance from the line to the points is minimized.)

 

 

 

 

 


A Line Graph Example

Generating a line graph is not that much different than with the scatterplot example. Instead of seaborn’s regplot, I use the plot function from matplotlib’s pyplot. All of the points are connected with line segments.

 

 

 

 


Plotting Math Functions

Plotting math functions in Python is similar to the code in the line graph plot section. To specify a domain, use numpy’s linspace() function.

 

Quadratic Parabola Example

In the quadratic parabola example, I specify the domain from -20 to 20 for x. The y variable is under the variable of quadratic_y.

Like in the line graph plot, pyplot’s plot function from matplotlib is used. To include math text in the plot, use plt.text() with the LaTeX like code in there.

 

 

 

A Cubic Function Example

In this example, I choose a simple cubic function with a domain from -10 to 10 for x.

 

 


References

 

  • https://www.tutorialspoint.com/seaborn/seaborn_quick_guide.htm
  • https://stackoverflow.com/questions/41968732/set-order-of-columns-in-pandas-dataframe
  • https://seaborn.pydata.org/generated/seaborn.barplot.html
  • https://stackoverflow.com/questions/31632637/label-axes-on-seaborn-barplot
  • https://seaborn.pydata.org/tutorial/distributions.html
  • https://python-graph-gallery.com/40-basic-scatterplot-seaborn/
  • https://python-graph-gallery.com/seaborn/
  • https://python-graph-gallery.com/120-line-chart-with-matplotlib/
  • https://glowingpython.blogspot.ca/2011/04/how-to-plot-function-using-matplotlib.html
  • https://python-graph-gallery.com/193-annotate-matplotlib-chart/

One thought on “Plots With Python & Seaborn

Leave a Reply