Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here are the steps to create a barplot with a threshold using matplotlib:

  1. Import the necessary libraries: import matplotlib.pyplot as plt and import numpy as np.

  2. Create a dataset with the values and labels for the x-axis: x = np.array(["A", "B", "C", "D"]) and y = np.array([10, 7, 15, 5]).

  3. Create a horizontal line indicating the threshold: threshold = 8 and plt.axhline(y=threshold, color="r", linestyle="--").

  4. Plot the barplot: plt.bar(x,y).

  5. Add labels and titles: plt.xlabel("Categories"), plt.ylabel("Values"), and plt.title("Barplot with Threshold").

  6. Show the plot: plt.show().

Here's the full code:

import matplotlib.pyplot as plt
import numpy as np

# Create dataset
x = np.array(["A", "B", "C", "D"])
y = np.array([10, 7, 15, 5])
threshold = 8

# Create horizontal line for threshold
plt.axhline(y=threshold, color="r", linestyle="--")

# Create barplot
plt.bar(x, y)

# Add labels and title
plt.xlabel("Categories")
plt.ylabel("Values")
plt.title("Barplot with Threshold")

# Show plot
plt.show()