Skip to content

Python Pandas - Visualization

Pandas provides convenient plotting capabilities directly on Series and DataFrame objects. These are essentially wrappers around the popular Matplotlib library, making common plots easy to generate without importing Matplotlib explicitly (though Matplotlib must be installed: pip install matplotlib).

The fundamental plotting method is .plot(). By default, it creates a line plot.

import pandas as pd
import numpy as np
# Make sure matplotlib is installed: pip install matplotlib
rng = np.random.default_rng(42)
df = pd.DataFrame(rng.standard_normal((10, 4)).cumsum(axis=0), # Use cumulative sum for more interesting lines
index=pd.date_range('2023-01-01', periods=10, freq='D'),
columns=['A', 'B', 'C', 'D'])
print("DataFrame:")
print(df)
# Generate the plot (opens in a separate window or inline in notebooks)
try:
plot = df.plot()
print("\nGenerating line plot...")
# In a script, you might need plot.figure.show() or save it
# plot.figure.savefig('basic_line_plot.png')
except ImportError:
print("\nMatplotlib not found. Please install it: pip install matplotlib")
except Exception as e:
print(f"\nPlotting error (อาจจะเกิดใน environments ที่ไม่มี display): {e}")

Description of Output:

This code generates a line plot. The DataFrame’s index (dates) is used for the x-axis, and each column (‘A’, ‘B’, ‘C’, ‘D’) is plotted as a separate colored line on the y-axis. Because the index consists of dates, Pandas automatically formats the x-axis labels appropriately for readability.

You can plot specific columns against each other using the x and y arguments within certain plot types (like scatter plots).

The .plot() method supports various plot types via the kind keyword argument:

  • kind='line' (default)
  • kind='bar' or kind='barh' for vertical or horizontal bar plots
  • kind='hist' for histograms
  • kind='box' for box plots
  • kind='kde' or kind='density' for Kernel Density Estimation plots
  • kind='area' for area plots
  • kind='pie' for pie charts
  • kind='scatter' for scatter plots

Alternatively, you can use the specific methods like df.plot.bar(), df.plot.hist(), etc.

Bar plots are useful for comparing values across different categories or groups.

import pandas as pd
import numpy as np
rng = np.random.default_rng(43)
df_bar = pd.DataFrame(rng.random((6, 4)),
index=['one', 'two', 'three', 'four', 'five', 'six'],
columns=pd.Index(['A', 'B', 'C', 'D'], name='Metrics'))
print("DataFrame for Bar Plot:")
print(df_bar)
try:
# Vertical Bar Plot
ax_bar = df_bar.plot(kind='bar')
# ax_bar = df_bar.plot.bar() # Equivalent
ax_bar.set_title('Vertical Bar Plot')
ax_bar.set_ylabel('Values')
print("\nGenerating vertical bar plot...")
# ax_bar.figure.show()
except Exception as e:
print(f"\nPlotting error: {e}")

Description of Output:

This generates a vertical bar plot. The index labels (‘one’ to ‘six’) appear on the x-axis. For each index label, there is a group of bars, one for each column (‘A’, ‘B’, ‘C’, ‘D’), showing their respective values.

To create a stacked bar plot, pass stacked=True:

# Using df_bar from the previous example
try:
# Stacked Vertical Bar Plot
ax_stacked = df_bar.plot.bar(stacked=True)
ax_stacked.set_title('Stacked Vertical Bar Plot')
ax_stacked.set_ylabel('Cumulative Values')
print("\nGenerating stacked vertical bar plot...")
# ax_stacked.figure.show()
except Exception as e:
print(f"\nPlotting error: {e}")

Description of Output:

This generates a stacked bar plot. For each index label on the x-axis, there is a single bar representing the sum of values from all columns (‘A’ through ‘D’). Segments within each bar, differentiated by color, show the contribution of each column to the total.

For horizontal bar plots, use kind='barh' or .plot.barh():

# Using df_bar from the previous example
try:
# Horizontal Stacked Bar Plot
ax_barh = df_bar.plot.barh(stacked=True)
ax_barh.set_title('Horizontal Stacked Bar Plot')
ax_barh.set_xlabel('Cumulative Values')
print("\nGenerating horizontal stacked bar plot...")
# ax_barh.figure.show()
except Exception as e:
print(f"\nPlotting error: {e}")

Description of Output:

This generates a horizontal stacked bar plot. The index labels are now on the y-axis, and the bars extend horizontally. Each bar is segmented by color, representing the cumulative contribution of each column.

Histograms visualize the distribution of numerical data by grouping values into bins and showing the frequency of values in each bin.

import pandas as pd
import numpy as np
rng = np.random.default_rng(44)
df_hist = pd.DataFrame({
'a': rng.standard_normal(1000) + 1,
'b': rng.standard_normal(1000),
'c': rng.standard_normal(1000) - 1
}, columns=['a', 'b', 'c'])
print("DataFrame for Histogram:")
print(df_hist.head())
try:
# Plot histograms for all columns on the same axes
ax_hist = df_hist.plot.hist(bins=20, alpha=0.5) # alpha for transparency
ax_hist.set_title('Histogram of Columns A, B, C')
ax_hist.set_xlabel('Value')
print("\nGenerating overlapping histograms...")
# ax_hist.figure.show()
except Exception as e:
print(f"\nPlotting error: {e}")

Description of Output:

This creates a plot with three overlapping histograms, one for each column (‘a’, ‘b’, ‘c’), using 20 bins. The alpha=0.5 makes the histograms semi-transparent, allowing visualization of overlaps. It shows the distribution shape for each column’s data.

To plot histograms for each column in separate subplots, you can iterate or use DataFrame’s built-in hist() method (which differs slightly from plot.hist()):

# Using df_hist from the previous example
try:
# Plot histograms in separate subplots
axes_hist_sep = df_hist.hist(bins=20, grid=False, figsize=(10, 4)) # Returns array of axes
# Access figure via axes: axes_hist_sep[0,0].figure.suptitle('Separate Histograms')
print("\nGenerating separate histograms...")
# axes_hist_sep[0,0].figure.show()
except Exception as e:
print(f"\nPlotting error: {e}")

Description of Output:

This uses the df.hist() method to generate a figure containing separate subplots, each displaying the histogram for one column (‘a’, ‘b’, or ‘c’) with 20 bins. This avoids overlap and makes individual distributions clearer.

Box plots (or box-and-whisker plots) are excellent for visualizing the distribution and summary statistics (median, quartiles, outliers) of numerical data, often comparing distributions across different groups or columns.

import pandas as pd
import numpy as np
rng = np.random.default_rng(45)
df_box = pd.DataFrame(rng.random((10, 5)), columns=['A', 'B', 'C', 'D', 'E'])
print("DataFrame for Box Plot:")
print(df_box.head())
try:
# Generate box plots for each column
ax_box = df_box.plot.box()
# ax_box = df_box.boxplot() # Similar alternative
ax_box.set_title('Box Plot of Columns A-E')
ax_box.set_ylabel('Value')
print("\nGenerating box plot...")
# ax_box.figure.show()
except Exception as e:
print(f"\nPlotting error: {e}")

Description of Output:

This code produces a figure with five box plots, one for each column (‘A’ through ‘E’). Each box shows the interquartile range (IQR) (25th to 75th percentile), the median (line inside the box), and ‘whiskers’ typically extending to 1.5 times the IQR from the box edges. Points beyond the whiskers may be plotted as individual outliers.

Area plots are like line plots but fill the area below the line(s), often used to show cumulative totals or contributions over time.

import pandas as pd
import numpy as np
rng = np.random.default_rng(46)
df_area = pd.DataFrame(rng.random((10, 4)), columns=['a', 'b', 'c', 'd'])
print("DataFrame for Area Plot:")
print(df_area.head())
try:
# Generate a stacked area plot (default)
ax_area = df_area.plot.area()
ax_area.set_title('Stacked Area Plot')
ax_area.set_ylabel('Cumulative Value')
print("\nGenerating stacked area plot...")
# ax_area.figure.show()
except Exception as e:
print(f"\nPlotting error: {e}")

Description of Output:

This generates a stacked area plot. The y-axis represents the cumulative sum of the column values at each index position on the x-axis. Each colored area represents the contribution of a specific column to the total stack.

Scatter plots are used to visualize the relationship between two numerical variables. Each point represents an observation.

import pandas as pd
import numpy as np
rng = np.random.default_rng(47)
df_scatter = pd.DataFrame(rng.random((50, 4)), columns=['a', 'b', 'c', 'd'])
print("DataFrame for Scatter Plot (first 5 rows):")
print(df_scatter.head())
try:
# Generate a scatter plot of column 'a' vs 'b'
ax_scatter = df_scatter.plot.scatter(x='a', y='b')
ax_scatter.set_title('Scatter Plot of Column B vs Column A')
ax_scatter.set_xlabel('Value of Column A')
ax_scatter.set_ylabel('Value of Column B')
print("\nGenerating scatter plot...")
# ax_scatter.figure.show()
except Exception as e:
print(f"\nPlotting error: {e}")

Description of Output:

This creates a scatter plot where each point’s horizontal position is determined by the value in column ‘a’ and its vertical position by the value in column ‘b’. It helps to visually inspect correlation or patterns between these two variables.

Pie charts represent proportions or percentages of a whole. They are best suited for a small number of categories.

import pandas as pd
import numpy as np
# Create a Series suitable for a pie chart (positive values)
series_pie = pd.Series(3 * np.abs(np.random.randn(4)), index=['a', 'b', 'c', 'd'], name='Proportions')
print("Series for Pie Chart:")
print(series_pie)
try:
# Generate a pie chart from the Series
# autopct formats the percentage display
ax_pie = series_pie.plot.pie(autopct='%.1f%%', figsize=(5, 5))
ax_pie.set_title('Pie Chart Example')
ax_pie.set_ylabel('') # Remove the default y-label (often the series name)
print("\nGenerating pie chart...")
# ax_pie.figure.show()
except Exception as e:
print(f"\nPlotting error: {e}")

Description of Output:

This code generates a pie chart from the series_pie. Each slice represents an index label (‘a’, ‘b’, ‘c’, ‘d’), and the size of the slice is proportional to the corresponding value in the Series. The percentages are displayed on the slices.

Note on Visualization Libraries: While Pandas’ built-in plotting is convenient for quick exploration, libraries like Seaborn (built on Matplotlib, offers more statistically-oriented plots and better aesthetics) and Plotly (creates interactive plots) offer more advanced features and customization. Consider exploring them for more complex visualization needs.