Visualizing Standard Deviation in Python

NIBEDITA (NS)
3 min readSep 19, 2024

Hey, guys! Welcome to the 5th Article in our Data Science and Analytics Series. In today’s article, we won’t solve any problem, instead today we’ll visualize the Standard Deviation of Sales using Python. Basically this is going to be the 2nd part of the previous Article.

So, here’s the Dataset from our previous problem.

Sales Data for Standard Deviation

For a quick recap, Standard Deviation is a measure of the Amount of Variation or Dispersion in a Set of Values. If I explain it in simple terms, it tells us that in our dataset, how much the values Deviate from the Mean/Average value.

Great! If you want, you can also watch the Video of this Visualization Step-by-step: 👇

How to Visualize Standard Deviation in Python?

Alright! Here, we’ll visualize using Matplotlib. So, let’s first import the necessary libraries.

import matplotlib.pyplot as plt
import numpy as np

We’re using Matplotlib for visualizing the Standard Deviation, but what are we using NumPy for? Well, we’ll be needing to calculate the Mean and Standard Deviation. And as this is our Visualization article, we won’t write complicated code just to find the Mean and Standard Deviation values. Why we have NumPy then? So, we’ll use it for calculating the values.

Okay, let’s now store the values.

days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
sales = [20, 35, 30, 25, 40]

Great! Now, we’ll use NumPy to find the Mean and Standard Deviation values.

meanVal = np.mean(sales)
stdVal = np.std(sales)

Okay, let’s start our visualization now. The very first thing we’ll do here is to set the figure size.

plt.figure(figsize=(10, 6))

Here, I have set the figrue size to 10 by 6 inches. It’s not fixed, you can set the figure to any size you want. Or else just skip this part, if you like the default one.

Let’s now add a bar plot using plt.bar().

plt.bar(days, sales, color='orange', alpha=0.7)

Here, I have set the bar color to orange, you can change to any color you like it to be. Also, I want it to be slightly transparent, that’s why I have set the alpha to 0.7. If you like deep orange, you can just skip this.

Now, we’ll add three horizontal lines to our plot. First one is for the Mean, 2nd and 3rd lines are for Standard Deviation, one above and other is below the Mean.

plt.axhline(meanVal, color='blue', linestyle='dashed', linewidth=2,
label=f'Mean: {meanVal}')

plt.axhline(meanVal+ stdVal, color='green', linestyle='dashed',
linewidth=2, label=f'Stdev: {stdVal:.2f}')

plt.axhline(meanVal- stdVal, color='green',
linestyle='dashed', linewidth=2)

Again, you can make changes to the styles as you like. For instance, let’s say you don’t want the lines to be of different styles, so just remove linestyle and it’ll set it to the default style.

Now, we’ll add our final labels to the Plot.

plt.xlabel("Days")
plt.ylabel("Products Sold")
plt.title("Standard Deviation of Products Sold Over Five Days")
plt.legend()
plt.show()

You can give any title you want. Also, if you don’t like legends, just skip it. But it’s good to add in the plot.

How to Visualize Standard Deviation in Python?
Visualization of Standard Deviation

And this is our final outcome. As I said earlier, the styles are not fixed. So, go ahead and try modifying things and see which visual excites you the most.

When writing your code, consider readability as well. Efficiency matters more than the length. So, be sure to write clean code.

Also here, write this block of code in one cell, if you are visualizing in Jupyter Notebook.

plt.figure(figsize=(10, 6))
plt.bar(days, sales, color='orange', alpha=0.7)


plt.axhline(meanVal, color='blue', linestyle='dashed', linewidth=2,
label=f'Mean: {meanVal}')

plt.axhline(meanVal+ stdVal, color='green', linestyle='dashed',
linewidth=2, label=f'Stdev: {stdVal:.2f}')

plt.axhline(meanVal- stdVal, color='green',
linestyle='dashed', linewidth=2)


plt.xlabel("Days")
plt.ylabel("Products Sold")
plt.title("Standard Deviation of Products Sold Over Five Days")
plt.legend()
plt.show()

If you’re doing it in another IDE, then there’s no need for that, but Jupyter Notebook is best for Data Visualization and Analysis. Else, it sometimes depends on your preferences as well. 😉

But for your Data Visualization and Analysis, I’ll still suggest Jupyter Notebook. 😄

And that’s all for today’s article. I hope this article was helpful. If you get any doubt, you can ask me.

Thanks for reading! 😊

--

--

NIBEDITA (NS)
NIBEDITA (NS)

Written by NIBEDITA (NS)

Tech enthusiast, Content Writer and lifelong learner!

No responses yet