- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 −
- Related Articles
- How to plot and work with NaN values in Matplotlib?
- How to create a Boxplot with Matplotlib?
- How to deal with warning “removed n rows containing missing values” while using ggplot2 in R?
- How to plot masked and NaN values in Matplotlib?
- Plotting animated quivers in Python using Matplotlib
- Python - How to fill NAN values with mean in Pandas?
- How can I plot NaN values as a special color with imshow in Matplotlib?
- Plotting a masked surface plot using Python, Numpy and Matplotlib
- Remove NaN values from a dataframe without fillna or Interpolate (Python Matplotlib)
- Interactive plotting with Python Matplotlib via command line
- Plotting with seaborn using the matplotlib object-oriented interface
- Gaussian filtering an image with NaN in Python Matplotlib
- Python Pandas - Fill missing columns values (NaN) with constant values
- Adding a scatter of points to a boxplot using Matplotlib
- How to deal with glm.fit error “NA/NaN/Inf” for logistic regression model in R?

Advertisements