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.
2024
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.
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()
Bar charts visualize data that is organized according to categories as a series of bars.
They are often used to compare different groups.
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()
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()
Heat maps display data through variations in coloring.
They are useful for visualizing the magnitude of a phenomenon as color in two dimensions.
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()
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.
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()
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)
# 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()
# 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()
## (0.0, 1.0)
# 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()