Practical Matplotlib Visualization Strategies for Data Analysis

Configuring Axis Ticks and Grids

Adjusting tick intervals improves readability on specific axes. For example, setting y-axis ticks at intervals of 5 units:

ax_secondary.set_yticks([val for val in range(0, 40, 5)])

Grid lines can be enabled to assist with data interpretation:

ax_primary.grid(linestyle='--', alpha=0.5)
ax_secondary.grid(linestyle='--', alpha=0.5)

Labels and titles provide context for the visualization:

ax_primary.set_xlabel("Time Interval")
ax_primary.set_ylabel("Temperature")
ax_primary.set_title("Shanghai Temp Variation 11:00-12:00")

ax_secondary.set_xlabel("Time Interval")
ax_secondary.set_ylabel("Temperature")
ax_secondary.set_title("Beijing Temp Variation 11:00-12:00")

Finally, render the plot:

plt.show()

Scatter Plots for Distribution Patterns

Scatter plots are effective for observing relationships between two continuous variables.

# Dataset preparation
coord_x = [225.98, 247.07, 253.14, 457.85, 241.58, 301.01, 20.67, 288.64,
  163.56, 120.06, 207.83, 342.75, 147.9 , 53.06, 224.72, 29.51,
  21.61, 483.21, 245.25, 399.25, 343.35]

coord_y = [196.63, 203.88, 210.75, 372.74, 202.41, 247.61, 24.9 , 239.34,
  140.32, 104.15, 176.84, 288.23, 128.79, 49.64, 191.74, 33.1 ,
  30.74, 400.02, 205.35, 330.64, 283.45]

# Canvas setup
plt.figure(figsize=(20, 8), dpi=80)

# Plot generation
plt.scatter(coord_x, coord_y)

# Display
plt.show()

Bar Charts for Statistical Comparison

Bar charts are suitabel for comparing discrete categories, such as movie box office revenue.

# Data preparation
categories = ['Thor: Ragnarok', 'Justice League', 'Murder on the Orient Express', 'Coco', 'Geostorm',
              'The Exorcist', 'Manhunt', '77 Days', 'Secret War', 'Beast', 'Others']
values = [73853, 57767, 22354, 15969, 14839, 8725, 8716, 8318, 7916, 6764, 52222]

# Canvas setup
plt.figure(figsize=(20, 8), dpi=80)

# Plot generation
indices = range(len(categories))
plt.bar(indices, values, color=['b', 'r', 'g', 'y', 'c', 'm', 'y', 'k', 'c', 'g', 'b'])

# Customize ticks
plt.xticks(indices, categories)

# Add title and grid
plt.title("Box Office Revenue Comparison")
plt.grid(linestyle="--", alpha=0.5)

# Display
plt.show()

Histograms for Frequency Distribution

Histograms differ from bar charts in several fundamental ways:

  1. Histograms display data distributtion, whereas bar charts compare magnitudes.
  2. The X-axis in a histogram represents quantitative data, while bar charts use categorical data.
  3. Histogram bars are contiguous (no gaps), while bar chart bars have spacing.
  4. Histogram bar widths can vary, whereas bar chart widths must be uniform.

Example: Movie Duration Distribution

# Data preparation
durations = [131, 98, 125, 131, 124, 139, 131, 117, 128, 108, 135, 138, 131, 102, 107, 114, 119, 128, 121, 142, 127, 130, 124, 101, 110, 116, 117, 110, 128, 128, 115, 99, 136, 126, 134, 95, 138, 117, 111, 78, 132, 124, 113, 150, 110, 117, 86, 95, 144, 105, 126, 130, 126, 130, 126, 116, 123, 106, 112, 138, 123, 86, 101, 99, 136, 123, 117, 119, 105, 137, 123, 128, 125, 104, 109, 134, 125, 127, 105, 120, 107, 129, 116, 108, 132, 103, 136, 118, 102, 120, 114, 105, 115, 132, 145, 119, 121, 112, 139, 125, 138, 109, 132, 134, 156, 106, 117, 127, 144, 139, 139, 119, 140, 83, 110, 102, 123, 107, 143, 115, 136, 118, 139, 123, 112, 118, 125, 109, 119, 133, 112, 114, 122, 109, 106, 123, 116, 131, 127, 115, 118, 112, 135, 115, 146, 137, 116, 103, 144, 83, 123, 111, 110, 111, 100, 154, 136, 100, 118, 119, 133, 134, 106, 129, 126, 110, 111, 109, 141, 120, 117, 106, 149, 122, 122, 110, 118, 127, 121, 114, 125, 126, 114, 140, 103, 130, 141, 117, 106, 114, 121, 114, 133, 137, 92, 121, 112, 146, 97, 137, 105, 98, 117, 112, 81, 97, 139, 113, 134, 106, 144, 110, 137, 137, 111, 104, 117, 100, 111, 101, 110, 105, 129, 137, 112, 120, 113, 133, 112, 83, 94, 146, 133, 101, 131, 116, 111, 84, 137, 115, 122, 106, 144, 109, 123, 116, 111, 111, 133, 150]

# Canvas setup
plt.figure(figsize=(20, 8), dpi=80)

# Plot generation
bin_width = 2
group_count = int((max(durations) - min(durations)) / bin_width)

plt.hist(durations, bins=group_count, density=True)

# Customize x-axis ticks
plt.xticks(range(min(durations), max(durations) + 2, bin_width))

plt.show()

Tags: python matplotlib data-visualization Plotting

Posted on Wed, 10 Jun 2026 16:05:42 +0000 by russellpehrson