python-matplotlib-pyplot

Introduction to Python Matplotlib Pyplot & Plotting

In This Article, You Will Know About Python Matplotlib.

Python Matplotlib Pyplot – Before moving ahead, let’s take a look at Python Matplotlib Intro

Table of Contents

Matplotlib Pyplot

The Matplotlib library works under submodule i.e., pyplot, and this is imported under the plt alias.

Follow the code given below to import matplotlib to get started with plotting the graph.

				
					import matplotlib.pyplot as plt
				
			

Now, the pyplot submodule will work under align plt.

Example: Draw a line on an illustration from the position (0,0) until position (6,250).

				
					import matplotlib.pyplot as plt
import numpy as np

x_points = np.array([0, 10])
y_points = np.array([0, 500])

plt.plot(x_points, y_points)
plt.show()

				
			
python-matplotlib-pyplot

As can be seen, it returned a graph based on the point given on x and y axis.

Plotting x and y points

The plot() function is used to make points (markers) in a diagram. By default, the plot() function draws a point that runs from one point.

It takes two arguments as parameter –

x-axis – Array having points on x-axis.

y-axis – Array having points on y-axis.

Example: If we are required for plotting a line that runs from (2,6) to (5,1) then we must connect two arrays, [2, 6], and [5, 1] into the function plot.

Example: Drawing a line in a diagram from point (12,6) to point (6,1).

				
					import matplotlib.pyplot as plt
import numpy as np

x_axis = np.array([12,6])
y_axis = np.array([6,1])

plt.plot(x_axis, y_axis)
plt.show()

				
			
python-matplotlib-pyplot

As shown above, it returned a graph based on the point given on x and y axis.

Plotting Without Line

To draw only point in a diagram, use shortcut parameter “o” which means “rings”.

				
					import matplotlib.pyplot as plt
import numpy as np

x_axis = np.array([12,6])
y_axis = np.array([6,1])

plt.plot(x_axis, y_axis, 'o')
plt.show()

				
			
python-matplotlib-pyplot

As a result, it returned a graph based on the point given on x and y axis.

Note: Learn additional information about markers in the next lesson.

Multiple Points

You are able to plot any number of points you want make sure you’ve got the exact number of points on both the axes.

Example: Draw a line in a diagram from position (1, 5) to (2, 6) then to (3, 7) and finally to position (4, 8).

				
					import matplotlib.pyplot as plt
import numpy as np

x_axis = np.array([1, 2, 3, 4])
y_axis = np.array([5, 6, 7, 8])

plt.plot(x_axis, y_axis)
plt.show()

				
			

As has been noted, it returned a graph based on the point given on both axes.

Default X-Points

If we do not define the x-axis points these will take the default values of 1, 2, 3, (etc. dependent on what length the y-points are.

Therefore, if we apply the same example from above and remove the x-points, the diagram would appear like this:

Example: Drawing a diagram without x-points.

				
					import matplotlib.pyplot as plt
import numpy as np

y_axis = np.array([5, 6, 7, 8])

plt.plot(y_axis)
plt.show()

				
			

As can be seen, it returned a graph based on the point given on x (x-axis assumed) and y axis.

Note: The x-points in the example above is [0, 1, 2, 3, 4].

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on