In this tutorial, we will learn how to plot the standard deviation in Python, which is a widely used statistical measure that conveys the amount of dispersion, or variation, within a dataset. By the end of this tutorial, you will be able to both calculate and plot the standard deviation using Python libraries such as **NumPy** and **Matplotlib**.

**Prerequisites:**

To follow this tutorial, you will need to have a basic understanding of Python programming and should have the following Python libraries installed:

**NumPy**: A library for the Python programming language, which adds support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.**Matplotlib**: A plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits.

You can install both libraries using the pip package manager with the following command:

1 |
pip install numpy matplotlib |

### Step 1: Import Required Libraries and Create Data

First, we need to import the required libraries and create some sample data to work with. For this tutorial, we’ll use an array of numbers generated using NumPy’s `array`

method.

1 2 3 4 |
import numpy as np import matplotlib.pyplot as plt data = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20]) |

### Step 2: Calculate the Mean and Standard Deviation

Now, we will calculate the mean and standard deviation of the data using NumPy’s `mean`

and `std`

methods, respectively.

1 2 |
mean = np.mean(data) std_dev = np.std(data) |

### Step 3: Create a Plot with Standard Deviation

In this step, we will create a scatter plot using Matplotlib, and then add error bars to represent the standard deviation. To achieve this, we will use the `errorbar`

method.

1 2 3 4 5 6 |
plt.scatter(np.arange(len(data)), data, label='Data') plt.errorbar(np.arange(len(data)), data, yerr=std_dev, linestyle='None', label='Standard Deviation', color='red', capsize=5) plt.xlabel('Index') plt.ylabel('Value') plt.legend() plt.show() |

The above code creates a scatter plot of the data points and adds red error bars of length equal to the standard deviation. The `capsize`

parameter determines the width of the caps on the error bars. The `label`

parameter provides labels for the data and standard deviation, which are displayed using the `legend`

method.

### Step 4: Customize the Plot (Optional)

You can further customize the plot by changing the colors, styles, and adding titles.

For example, you can change the color and style of the error bars:

1 |
plt.errorbar(np.arange(len(data)), data, yerr=std_dev, linestyle=':', label='Standard Deviation', color='green', capsize=5) |

You can also add a title to the plot using the `title`

method:

1 |
plt.title('Scatter Plot with Standard Deviation') |

### Full Code

Here is the full code for plotting standard deviation in Python:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import numpy as np import matplotlib.pyplot as plt data = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20]) mean = np.mean(data) std_dev = np.std(data) plt.scatter(np.arange(len(data)), data, label='Data') plt.errorbar(np.arange(len(data)), data, yerr=std_dev, linestyle='None', label='Standard Deviation', color='red', capsize=5) plt.xlabel('Index') plt.ylabel('Value') plt.legend() plt.title('Scatter Plot with Standard Deviation') plt.show() |

And this is the output of the code: