- 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 sort a boxplot by the median values in Pandas?
To sort a boxplot by the median values in Pandas, we can take the following steps
Steps
Set the figure size and adjust the padding between and around the subplots.
Create a Pandas dataframe of two-dimensional, size-mutable, potentially heterogeneous tabular data, with three columns.
Group the dataframe elements by marks and dob.
Find the median of the dataframe.
Get the sorted values of the median.
Create a box plot from the DataFrame columns.
To display the figure, use Show() method.
Example
import pandas as pd import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame([ [23, 'James', 12], [39, 'Jimmy', 27], [56, 'Jack', 69], [60, 'Tom', 96], [80, 'Tim', 79] ], columns=['marks', 'names', 'dob']) g = df.groupby(["marks", "dob"]) df = pd.DataFrame({col: val['dob'] for col, val in g}) median = df.median() median.sort_values(ascending=False, inplace=True) df = df[median.index] df.boxplot() plt.show()
Output
It will produce the following output −
Advertisements