Step 1: Import Libraries
First, you need to import the necessary libraries. Weβll use matplotlib.pyplot
for plotting.
import matplotlib.pyplot as plt
Step 2: Prepare Your Data
Create lists or arrays for the categories (x-axis) and their corresponding values (y-axis). Hereβs a simple example:
= ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
categories = [-22.89, -20.7, -20.69, -11.76, -0.8, 8.59, 11.22, 7.23, -0.11, -10.54, -18.34, -21.44] values
Step 3: Create the Bar Plot
Use the bar()
function from pyplot
to create the bar plot. Pass the categories and values as arguments.
plt.bar(categories, values)
Step 4: Add Labels and Title
You can enhance your plot by adding a title and labels for the x-axis and y-axis.
'Categories')
plt.xlabel('Values')
plt.ylabel('Simple Bar Plot') plt.title(
Step 5: Display the Plot
Finally, use plt.show()
to display the plot.
plt.show()
Complete Code Example
Hereβs the complete code to create a simple bar plot:
import matplotlib.pyplot as plt
# Data for the plot
= ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
categories = [-22.89, -20.7, -20.69, -11.76, -0.8, 8.59, 11.22, 7.23, -0.11, -10.54, -18.34, -21.44]
values
# Create the bar plot
plt.bar(categories, values)
# Add labels and title
'Months')
plt.xlabel('Average Temperature, deg-C')
plt.ylabel('Toolik Lake LTER Average Temperatures, 2008-2019')
plt.title(
# Display the plot
plt.show()
Customizing the Bar Plot
You can further customize your bar plot with additional options:
Color: Set the color of the bars using the
color
parameter.='skyblue') plt.bar(categories, values, color
Width: Adjust the width of the bars using the
width
parameter.=0.5) plt.bar(categories, values, width
Add Grid: Make the plot easier to read by adding a grid.
='y', linestyle='--', alpha=0.7) plt.grid(axis
Horizontal Bar Plot: Use
barh()
for horizontal bar plots.='skyblue') plt.barh(categories, values, color
Example with Customizations
import matplotlib.pyplot as plt
# Data for the plot
= ['Category A', 'Category B', 'Category C', 'Category D']
categories = [23, 17, 35, 29]
values
# Create the bar plot with customizations
='skyblue', width=0.5)
plt.bar(categories, values, color
# Add labels and title
'Categories')
plt.xlabel('Values')
plt.ylabel('Simple Bar Plot')
plt.title(
# Add grid
='y', linestyle='--', alpha=0.7)
plt.grid(axis
# Display the plot
plt.show()