

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 color a Seaborn boxplot based on DataFrame column name in Matplotlib?
To color a Seaborn boxplot based on dataframe column name, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Make a Pandas dataframe with two columns, col1 and col2.
- Make a boxplot with horizontal orientation.
- Get the boxes artists.
- Iterate the boxes and set the facecolor of the box.
- To display the figure, use show() method.
Example
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame( [[2, 4], [7, 2] ], columns=['col1', 'col2']) ax = sns.boxplot(data=df, orient='h') boxes = ax.artists for i, box in enumerate(boxes): if 'col1' in df.columns[i]: box.set_facecolor('r') else: box.set_facecolor('b') plt.show()
Output
- Related Questions & Answers
- How to export to PDF a graph based on a Pandas dataframe in Matplotlib?
- Rotate xtick labels in Seaborn boxplot using Matplotlib
- Select rows from a Pandas DataFrame based on column values
- Deleting a DataFrame row in Python Pandas based on column value
- Creating a Pandas dataframe column based on a given condition in Python
- Set Max value for color bar on Seaborn heatmap using Matplotlib
- Plot a polar color wheel based on a colormap using Python/Matplotlib
- Python Pandas - Draw a boxplot for each numeric variable in a DataFrame with Seaborn
- How to set the range of Y-axis for a Seaborn boxplot using Matplotlib?
- How to create a boxplot of single column in R data frame with column name?
- How to change the color of data points based on some variable in Matplotlib?
- How to edit the properties of whiskers, fliers, caps, etc. in a Seaborn boxplot in Matplotlib?
- Plotting error bars from a dataframe using Seaborn FacetGrid (Matplotlib)
- How to plot a dashed line on a Seaborn lineplot in Matplotlib?
- How to create a stacked bar chart for my DataFrame using Seaborn in Matplotlib?
Advertisements