Create a Table using pandas.plotting.table() method

The code starts with importing packages, we load the iris dataset from sklearn.datasets, next step is grouping data to form a 2-d dataset. after that, we plot bar plots for each species and create a table using the pandas.plotting.table() method.

Python3




# import packages and modules
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from pandas.plotting import table
  
# loading the iris dataset
iris = load_iris()
  
# creating a 2 dimensional dataframe out of the given data
iris_df = pd.DataFrame(data=np.c_[iris['data'],
                                  iris['target']], 
                       columns=iris['feature_names'] + ['target'])
  
# grouping data and calculating average
grouped_dataframe = iris_df.groupby('target').mean().round(1)
grouped_dataframe['species_name'] = ['setosa', 'versicolor', 'virginica']
  
# plotting data
ax = plt.subplot(211)
plt.title("Iris Dataset Average by Plant Type")
plt.ylabel('Centimeters (cm)')
  
ticks = [4, 8, 12, 16]
a = [x - 1 for x in ticks]
b = [x + 1 for x in ticks]
  
plt.xticks([])
  
plt.bar(a, grouped_dataframe.loc[0].values.tolist()[
        :-1], width=1, label='setosa')
plt.bar(ticks, grouped_dataframe.loc[1].values.tolist()[
        :-1], width=1, label='versicolor')
plt.bar(b, grouped_dataframe.loc[2].values.tolist()[
        :-1], width=1, label='virginica')
  
plt.legend()
plt.figure(figsize=(12, 8))
table(ax, grouped_dataframe.drop(['species_name'], axis=1), loc='bottom')


Output:

The grouped dataframe looks as:

The plot looks as:



How to Create a Table with Matplotlib?

In this article, we will discuss how to create a table with Matplotlib in Python.

Similar Reads

Method 1: Create a Table using matplotlib.plyplot.table() function

In this example, we create a database of average scores of subjects for 5 consecutive years. We import packages and plotline plots for each consecutive year. A table can be added to Axes using matplotlib.pyplot.table(). We can plot the table by taking columns on the x-axis and the y-axis for values....

Method 2: Create a Table using pandas.plotting.table() method

...