How To Display A Plot In Python

by Sudipta Deb | Dec 9, 2024 | Data Science, Data Visualization, Python | 0 comments

Sudipta Deb

Sudipta Deb

Founder of Technical Potpourri, Co-Founder of Shrey Tech, Enterprise Cloud Architect

Matplotlib.Pyplot is a compilation of utilities that enables matplotlib to operate similarly to MATLAB. Each pyplot function modifies a figure: for instance, it may generate a figure, establish a plotting area within a figure, plot lines in a plotting region, or embellish the plot with labels, among other actions.

In matplotlib.pyplot, various states are maintained across function calls, allowing it to monitor elements such as the current figure and plotting area, with plotting functions directed to the current Axes (note that "Axes" with an uppercase 'A' refers to the Axes concept, a fundamental component of a figure, rather than merely the plural of axis).

The form you have selected does not exist.

Sample Code

I normally put all my codes in my personal Github repository, so that I can easily share with others. Here is the Github Code Repo where you can find the code samples used in this blog post.

plot_function.ipynb

Create A Simple Plot

The most basic example is the plot() method to represent values as x,y coordinates in a data visualization. In this instance, plot() requires two parameters to define plot coordinates:

  • Parameter for an array of X-axis coordinates.
  • Parameter for an array of Y-axis coordinates.

A line extending from the coordinates (3, 1) to (10, 15) is represented by generating two arrays: (3, 10) and (1, 15).

Customize It With Marker & LineStyle

Marker and Linestyle are matplotlib keywords that can be used to customize the appearance of data in a plot without modifying data values.

  • Marker is an argument used to label each data value in a plot with a ‘marker‘.
  • Linestyle is an argument used to customize the appearance of lines between data values, or else remove them altogether.

In this example, each data value is labeled with the letter “X”, and given a dashed linestyle “--”

Scatterplot

Scatterplot is used to display data values as a collection of x,y coordinates represented by dots. In the below example, 2 arrays that represents the x and y coordinates are plotted using the Scatterplot.

Multiple Data Sets in One Plot

We can plot multiple data sets in one plot like below -

Subplots

We can use the Subplot to visualize more complex figures that contain more than one plat. In the below example, multiple axes are enclosed in one figure and displayed in subplots.

Final Thoughts

In summary, the matplotlib.pyplot.plot() method in Python is an essential instrument for generating diverse 2D charts, such as line plots and scatter plots. Its adaptability enables users to tailor plots by designating data points, line types, markers, and colors. The function offers versatility in plot layout and data management with optional parameters like 'fmt' and 'data.' Moreover, the provided Line2D objects facilitate additional editing and analysis of the plotted data. Matplotlib’s plot() function is an essential element in data visualization, providing an accessible interface for generating informative and aesthetically pleasing graphs in Python.

Disclaimer

This article is not endorsed by Salesforce, Google, or any other company in any way. I shared my knowledge on this topic in this blog post. Please always refer to Official Documentation for the latest information.

0 Comments

Leave a Reply

Written by Sudipta Deb

Enterprise Cloud Architect, Content Creator, 20x Salesforce Certified, 1x Google Cloud Certified, 2x Copado Certified

Related Posts

4 Important Probability Distributions – Normal, Binomial, Poisson, Bernoulli

4 Important Probability Distributions – Normal, Binomial, Poisson, Bernoulli

A probability distribution specifies the likelihood of all possible outcomes of a random variable. The distribution may be classified as either continuous or discrete based on the values assumed by a random variable. Various forms of probability distributions exist. In this post, I will examine Normal, Binomial, Poisson and Bernoulli distribution and demonstrate how to utilize Python to plot it.

read more...

0 Comments

Leave a Reply