#!/usr/bin/env python # coding: utf-8 # In[1]: get_ipython().run_line_magic('matplotlib', 'inline') # # Stacked Barplot # A function to conveniently plot stacked bar plots in matplotlib using pandas `DataFrame`s. # > from mlxtend.general_plotting import category_scatter # ## Overview # A matplotlib convenience function for creating barplots from DataFrames where each sample is associated with several categories. # ### References # # - - # ## Example 1 - Stacked Barplot from Pandas DataFrames # In[2]: import pandas as pd s1 = [1.0, 2.0, 3.0, 4.0] s2 = [1.4, 2.1, 2.9, 5.1] s3 = [1.9, 2.2, 3.5, 4.1] s4 = [1.4, 2.5, 3.5, 4.2] data = [s1, s2, s3, s4] df = pd.DataFrame(data, columns=['X1', 'X2', 'X3', 'X4']) df.columns = ['X1', 'X2', 'X3', 'X4'] df.index = ['Sample1', 'Sample2', 'Sample3', 'Sample4'] df # By default, the index of the `DataFrame` is used as column labels, and the `DataFrame` columns are used for the plot legend. # In[3]: import matplotlib.pyplot as plt from mlxtend.plotting import stacked_barplot fig = stacked_barplot(df, rotation=45, legend_loc='best') # ## API # In[1]: with open('../../api_modules/mlxtend.plotting/stacked_barplot.md', 'r') as f: print(f.read())