PANDAS AND DATA VISUALIZATION
The following subsections contain Python-based code samples that use Pandas in order to render the following types of charts and graphs:
- bar chart
- horizontal stacked bar chart
- vertical stacked bar chart
- nonstacked area chart
- stacked area chart
Pandas and Bar Charts
Listing 4.1 displays the contents of pandas_barchart.py that shows you how to render a bar chart in Pandas.
LISTING 4.1: pandas_barchart.py
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.rand(16,3), columns=['X1','X2','X3'])
print("First 5 rows:")
print(df.head())
print()
print("Diff of first 5 rows:")
print(df.diff().head())
print()
# bar chart:
ax = df.plot.bar()
plt.show()
Listing 4.1 starts with import statements and then initializes the Pandas DataFrame df1 with a set of data values. Launch the code in Listing 4.1 and you will see the following output:
First 5 rows:
X1 ...