Make Scatter Plot From Set of Points in Python Tuples
Now we’ll look at an example that shows how to use scatter and how scatter values can be passed to a function as a tuple argument. Assume your function takes four and five arguments each argument will be passed as a separate single data point or value to plot the scatter chart. Let’s see the implementation of it in Python.
What is Scatter in Python?
Scatter plots are used to observe the relationship between variables and use dots to represent the relationship between them. The scatter() method in the Matplotlib library is used to draw a scatter plot. Scatter plots are widely used to represent relations among variables and how a change in one affects the other.
Step 1: Creating Tuple Data
Suppose you have two tuple values, the year in which one was admitted to engineering college and the year in which he/she will complete his/her degree. Lets store this data in the data variable.
Step 2: Unpacking Values
Here, we’ll use the zip() function to get the start_year and end_year values. We will pass the tuple as an argument to this function, and it will extract the tuple, perform the task, and return the result.
(2015, 2015, 2018, 2020, 2017, 2016, 2015, 2019, 2022, 2021) (2018, 2017, 2021, 2023, 2020, 2019, 2018, 2022, 2025, 2024)
Step 3: Plotting Scatter Graph
As you can see using Python Zip() to unpack the values and returns a tuple object for starting and ending years. Here * denotes that the argument is variable length. You may also use matplotlib.pyplot package and can plot scatter charts using the values in a graph using the scatter function.
Please Login to comment...