2024

Scatter Plots

  • Scatter plots display the relationship between two numerical variables.

  • Each data point represents an observation in the dataset, with its position determined by the values of the two variables.

Example: code

import matplotlib.pyplot as plt

x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 100, 86, 103, 87, 94, 78, 77, 85, 86]

plt.scatter(x, y)
plt.xlabel('X Value')
plt.ylabel('Y Value')
plt.title('Scatter Plot Example')
plt.show()

Example: plot

Bar Charts

  • Bar charts visualize data that is organized according to categories as a series of bars.

  • They are often used to compare different groups.

Example: code

categories = ['Category 1', 'Category 2', 'Category 3']
values = [23, 45, 56]

plt.bar(categories, values)
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Chart Example')
plt.show()

Example: plot

Histograms

  • Histograms are used to represent the distribution of a numerical variable, showing how many observations fall into each range of values.

Example: code

import numpy as np

data = np.random.randn(1000)

plt.hist(data, bins=30)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram Example')
plt.show()

Example: plot

Heat Maps

  • Heat maps display data through variations in coloring.

  • They are useful for visualizing the magnitude of a phenomenon as color in two dimensions.

Example: code

import numpy as np

data = np.random.randn(100, 100)

fig, ax = plt.subplots(1, 1, squeeze=False)
ax[0, 0].imshow(data, cmap='hot', interpolation='nearest')
ax[0, 0].invert_yaxis()
plt.title('Heat Map Example')
plt.show()

Example: plot

3D Scatter Plots

  • 3D scatter plots display data points on three axes (x, y, and z), allowing the examination of the relationships between three variables.

  • Requires a bit more setup to use in Matplotlib through the mpl_toolkits.mplot3d module.

Example: code

from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.random.randn(100, 100)
y = np.random.randn(100, 100)
z = np.random.randn(100, 100)

ax.scatter(x, y, z, alpha=0.1)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
ax.set_title('3D Scatter Plot Example')

plt.show()

Example: plot

Practice

  • Generate synthetic data for 50 participants.
  • For each participant, simulate response time (RT) in milliseconds and accuracy (0 = incorrect, 1 = correct) in two conditions: Control (C) and Experimental (E).
  • Scatter Plot: Display the relationship between RTs in the Control and Experimental conditions for each participant.
  • Bar Chart: Show the average accuracy in the Control and Experimental conditions.
  • Histogram: Visualize the distribution of RT differences (Experimental - Control) across participants.
  • Include appropriate titles, axis labels, and legends where relevant.
  • Use distinct colors or markers to differentiate between conditions.

Answer: Data

import matplotlib.pyplot as plt
import numpy as np

# Set the seed for reproducibility
np.random.seed(42)

# Data Preparation
participants = np.arange(1, 51)  # 50 participants
rt_control = np.random.normal(500, 50, 50)  # Control condition RTs

# Experimental condition RTs, generally faster
rt_experimental = rt_control - np.random.normal(20, 10, 50)  

# Control condition accuracy
accuracy_control = np.random.binomial(1, 0.8, 50)  

# Experimental condition accuracy, slightly better
accuracy_experimental = np.random.binomial(1, 0.85, 50)  

Answer: Scatter

# Scatter Plot: RTs in Control vs. Experimental
plt.figure(figsize=(8, 6))
plt.scatter(rt_control, rt_experimental, c='blue', label='Participants')
plt.plot([400, 600], [400, 600], 'k--', label='Equality line')  # Equality line for reference
plt.title('Response Times: Control vs. Experimental')
plt.xlabel('Control RT (ms)')
plt.ylabel('Experimental RT (ms)')
plt.legend()
plt.grid(True)
plt.show()

Answer: Scatter

Answer: Bar

# Bar Chart: Average Accuracy in Conditions
avg_accuracy_control = np.mean(accuracy_control)
avg_accuracy_experimental = np.mean(accuracy_experimental)
conditions = ['Control', 'Experimental']
accuracies = [avg_accuracy_control, avg_accuracy_experimental]

plt.bar(conditions, accuracies, color=['green', 'orange'])
plt.title('Average Accuracy by Condition')
plt.ylabel('Accuracy Rate')
plt.ylim(0, 1)
plt.show()

Answer: Bar

## (0.0, 1.0)

Answer: Histogram

# Histogram: RT Differences
rt_differences = rt_experimental - rt_control
plt.hist(rt_differences, bins=10, color='purple')
plt.title('Distribution of Response Time Differences (E - C)')
plt.xlabel('RT Difference (ms)')
plt.ylabel('Frequency')
plt.show()

Answer: Histogram