Data visualization using pyplot: line chart, pie chart and bar chart - Formula Sheet
Matplotlib Pyplot Core Syntax and Parameter Formulas
Data visualization in Python relies on standard programmatic structures. Below are the key functional syntaxes and their detailed parameter definitions used to create charts in Matplotlib.
1. Line Plot Function
Syntax: plt.plot(x, y, label=None)
- x: An ordered sequence (list or array) representing horizontal axis values. If omitted, indices default to 0, 1, 2, ..., n-1.
- y: An ordered sequence of the same length representing vertical axis values.
- label: An optional string argument that assigns a descriptive name to the line, used by the legend to identify the dataset.
2. Bar Chart Function
Syntax: plt.bar(x_positions, heights)
- x_positions: A numerical sequence that sets the coordinate markers of the bars on the horizontal axis.
- heights: A corresponding sequence of numeric values determining how tall each categorical bar should be.
3. Pie Chart Function
Syntax: plt.pie(sizes, labels=None, autopct=None)
- sizes: A list of positive numeric values representing the proportional volume of each slice relative to the total sum.
- labels: A list of string descriptors corresponding to each slice of the pie chart.
- autopct: A python string formatting code (e.g.,
"%.2f") that instructs the function to compute and render the percentage slice values on top of each sector.
4. Horizontal Axis Label Mapping
Syntax: plt.xticks(ticks, labels)
- ticks: A list of index coordinates indicating where descriptive text should be placed along the horizontal axis.
- labels: A corresponding list of string names mapped to each index point in the ticks sequence.
5. Graphic Window Management and Annotations
- plt.xlabel(label_string): Sets a string descriptor for the horizontal axis.
- plt.ylabel(label_string): Sets a string descriptor for the vertical axis.
- plt.title(title_string): Assigns an overall descriptive heading to the plotting window.
- plt.legend(): Draws a box containing names of the dataset labels inside the plot space.
- plt.show(): Renders and displays the active graphics window to the user.
More for this chapter
Book Back Questions9 textbook MCQs · solved
Additional MCQs15 extra MCQs · solved
Practice TestInteractive · instant score
Book Back TestTest yourself on the textbook set
Additional MCQ TestTest yourself on the extra set
Study NotesConcepts & methods
More chapters in Computer Science
View all
1 Function
2 Data Abstraction
3 Scoping
4 Algorithmic Strategies
5 Python -Variables and Operators
6 Control Structures
7 Python functions
8 Strings and String manipulation
9 Lists, Tuples, Sets and Dictionary
10 Python Classes and objects
11 Database Concepts
12 Structured Query Language (SQL)
13 Python and CSV files
14 Importing C++ programs in Python.
15 Data manipulation through SQL