How to plot true/false or active/deactive data in Matplotlib?

To plot true/false or active/deactive data in Matplotlib, we can visualize boolean values using different plotting methods. This is useful for displaying binary states, activity patterns, or presence/absence data.

Using imshow() for 2D Boolean Data

The imshow() method is ideal for displaying 2D boolean arrays as heatmaps ?

import matplotlib.pyplot as plt
import numpy as np

# Set figure parameters
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create random boolean data
data = np.random.random((20, 20)) > 0.5

# Create figure and plot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(data, aspect='auto', cmap="copper", interpolation='nearest')
ax.set_title('Boolean Data Visualization')

plt.show()

Using Different Color Maps

Different colormaps can better represent binary states ?

import matplotlib.pyplot as plt
import numpy as np

# Create sample boolean data
activity_data = np.random.choice([True, False], size=(15, 15), p=[0.3, 0.7])

fig, axes = plt.subplots(1, 3, figsize=(12, 4))

# Different colormaps for boolean data
cmaps = ['binary', 'RdYlGn', 'coolwarm']
titles = ['Binary', 'Red-Yellow-Green', 'Cool-Warm']

for i, (cmap, title) in enumerate(zip(cmaps, titles)):
    axes[i].imshow(activity_data, cmap=cmap, aspect='auto')
    axes[i].set_title(f'{title} Colormap')
    axes[i].set_xlabel('X Position')
    axes[i].set_ylabel('Y Position')

plt.tight_layout()
plt.show()

Line Plot for Time Series Boolean Data

For boolean data over time, line plots work well ?

import matplotlib.pyplot as plt
import numpy as np

# Create time series boolean data
time = np.arange(0, 100)
active_status = np.random.choice([True, False], size=100, p=[0.4, 0.6])

plt.figure(figsize=(10, 4))
plt.plot(time, active_status.astype(int), 'o-', linewidth=2, markersize=4)
plt.fill_between(time, active_status.astype(int), alpha=0.3)
plt.xlabel('Time')
plt.ylabel('Status')
plt.title('Active/Inactive Status Over Time')
plt.yticks([0, 1], ['Inactive', 'Active'])
plt.grid(True, alpha=0.3)
plt.show()

Scatter Plot for Boolean Categories

Scatter plots can show boolean data with different markers ?

import matplotlib.pyplot as plt
import numpy as np

# Create sample data
x = np.random.randn(50)
y = np.random.randn(50)
status = np.random.choice([True, False], size=50)

plt.figure(figsize=(8, 6))
# Plot True values
plt.scatter(x[status], y[status], c='green', marker='o', s=60, label='Active', alpha=0.7)
# Plot False values  
plt.scatter(x[~status], y[~status], c='red', marker='x', s=60, label='Inactive', alpha=0.7)

plt.xlabel('X Values')
plt.ylabel('Y Values')
plt.title('Boolean Data as Scatter Plot')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

Comparison of Methods

Method Best For Advantages
imshow() 2D boolean arrays Clear heatmap visualization
Line plot Time series data Shows trends over time
Scatter plot Categorical data Shows individual data points

Conclusion

Use imshow() for 2D boolean arrays, line plots for time series, and scatter plots for categorical boolean data. Choose colormaps like 'binary' or 'RdYlGn' for clear true/false visualization.

---
Updated on: 2026-03-25T22:53:13+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements