Basic_Plotting
Basic_Plotting
a. Line Chart
b. Bar Chart
Line charts are particularly useful for tracking changes over time,
illustrating the progression of variables, or identifying correlations
between different datasets. In a line chart, data points are connected
by lines, making it easy to observe the overall trajectory.
1|Page
pecific categories. In a bar chart, each category is represented by a
bar, the length of which corresponds to the value it represents.
Let’s learn to create line and bar charts using simple Python code..
a. Creating Line Charts: We can create line charts using plot() function.
Example Code:
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
Output:
The code begins by importing the pyplot
module from the matplotlib library and
aliasing it as plt.
2|Page
b. Creating Bar Charts: Bar charts are useful for comparing values across
different categories. The bar() function is used for making Bar charts.
Example Code:
# Sample data
categories = ['A', 'B', 'C', 'D']
values = [3, 7, 2, 5]
Output:
In the code, two lists named
categories and values, are
defined. The list categories
contains labels for different
categories (e.g., 'A', 'B', 'C', 'D'),
and values contains
corresponding numerical
values. The bar function from
the pyplot module is used to
create a bar chart. It takes categories as the x-axis labels and values
as the heights of the bars.
As visible in the bar chart, each category is associated with a bar, and
the height of the bar represents the value for that category. The bar
function from the pyplot module is used to create a bar chart. It takes
categories as the x-axis labels and values as the heights of the bars.
Each category is associated with a bar, and the height of the bar
represents the value for that category.
3|Page
Customizing Plots with Matplotlib:
4|Page
Customization Options:
5|Page
Example Code:
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Show a legend
plt.legend()
Output:
6|Page