1 | initial version |
Here are the steps to create a barplot with a threshold using matplotlib:
Import the necessary libraries: import matplotlib.pyplot as plt
and import numpy as np
.
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])
.
Create a horizontal line indicating the threshold: threshold = 8
and plt.axhline(y=threshold, color="r", linestyle="--")
.
Plot the barplot: plt.bar(x,y)
.
Add labels and titles: plt.xlabel("Categories")
, plt.ylabel("Values")
, and plt.title("Barplot with Threshold")
.
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()