- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to deal with NaN values while plotting a boxplot using Python Matplotlib?
To deal with NaN value while plotting a boxplot using Python, we can take the following steps −
Steps
Set the figure size and adjust the padding between and around the subplots.
Initialize a variable N for data samples and for range.
Next create the random spread, center's data, flier high and low, get the concatenated data, and the filtered data.
Create a box plot using boxplot() method.
To display the figure, use show() method.
Example
import matplotlib.pyplot as plt import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Data samples N = 10 # Random spread spread = np.random.rand(N) # Center's data center = np.ones(N) # Flier high and low fh = np.random.rand(N)+N fl = np.random.rand(N)-N # Concatenated data data = np.concatenate((spread, center, fh, fl), 0) data[5] = np.NaN # Filtered data filtered_data = data[~np.isnan(data)] # Plot the boxplot plt.boxplot(filtered_data) plt.show()
Output
It will produce the following output −
Advertisements