Matplotlib python library Basics
👉Part 13: Introduction To Python Libraries👈
matplotlib
library in Python.First, make sure you have matplotlib
installed. If you don't have it yet, you can install it using pip
:
import matplotlib.pyplot as plt
Now, let's create a bar chart to visualize the sales data for different products:
# Sample data for product sales products = ['Product A', 'Product B', 'Product C', 'Product D'] sales = [1200, 800, 1500, 1000] # Create a bar chart plt.bar(products, sales, color='skyblue') # Add labels and title plt.xlabel('Products') plt.ylabel('Sales') plt.title('Product Sales Data') # Show the plot plt.show()
This code snippet uses matplotlib
to create a bar chart with four products on the x-axis and their respective sales on the y-axis. The plt.bar()
function is used to plot the data. We set the color of the bars to 'skyblue' for a visually appealing appearance. The plt.xlabel()
, plt.ylabel()
, and plt.title()
functions are used to add labels and a title to the plot.
To see the chart, run this code in a Python environment. A window will pop up displaying the bar chart with the sales data for each product. You can further customize the chart by exploring various parameters in matplotlib
or use other libraries like seaborn
and plotly
for different types of visualizations.