Python matplotlib draw points

How to plot a point on the python graph

To draw a point on the graph with the python language, you can use the pyplot methods of the matplotlib module.

pyplot.plot(x,y)
pyplot.show()

The plot () method draws a point on the Cartesian diagram.

The show () method displays the graph.

A practical example

Import the pyplot module of matplotlib

>>> from matplotlib import pyplot as plt

Draw a circular point (marker = «o») of red color (color = «red») on the Cartesian plane to the coordinates x, y (2,3) by plot() function.

>>> plt.plot(2,3, marker=»o», color=»red»)

Display the graph on the screen by show() method.

The graph is displayed on the screen. In the graph there is a red round point at the coordinates (2,3).

Types of markers

To change dot marker use the marker attribute.

There are different types of markers

ch. descrizione
‘.’ point marker
‘,’ pixel marker
‘o’ circle marker
‘v’ triangle_down marker
‘^’ triangle_up marker
triangle_left marker
‘>’ triangle_right marker
‘1’ tri_down marker
‘2’ tri_up marker
‘3’ tri_left marker
‘4’ tri_right marker
‘s’ square marker
‘p’ pentagon marker
‘*’ star marker
‘h’ hexagon1 marker
‘H’ hexagon2 marker
‘+’ plus marker
‘x’ x marker
‘D’ diamond marker
‘d’ thin_diamond marker
‘|’ vline marker
‘_’ hline marker

Types of colors

To change dot color use the color attribute.

The following colors are supported

ch. colore
‘b’ blue
‘g’ green
‘r’ red
‘c’ cyan
‘m’ magenta
‘y’ yellow
‘k’ black
‘w’ white

It is possible to indicate a custom color using hex codes (‘#00FF11’).

Источник

How to plot points in matplotlib with Python

In this article, we will learn “How to plot points in matplotlib with Python”. For this, we have to implement two popular modules of Python in the field of plotting graph or figure named “matplotlib” and “numpy“. The main motto of this article is to learn how to plot any point or any graph(scattered point of graph) in matplotlib using Python.

Plotting of points in matplotlib with Python

There is a method named as “scatter(X,Y)” which is used to plot any points in matplotlib using Python, where X is data of x-axis and Y is data of y-axis.

Let’s understand this with some example:-

# importing two required module import numpy as np import matplotlib.pyplot as plt # Creating a numpy array X = np.array([1]) Y = np.array([5]) # Plotting point using sactter method plt.scatter(X,Y) plt.show()

plot one point in matplotlib with Python

In the above example, the first step is to import two modules of Python named as numpy and matplotlib by these two lines of codes:-

and then we created a numpy array and stored in a variable named as X and then created another numpy array and stored this in another variable named as Y. We stored only one value in X and Y, since we have to plot a single point in this example. Then we used the “plt.scatter(X,Y)” and “plt.show()” to plot that required point.

# importing two required module import numpy as np import matplotlib.pyplot as plt # Creating a numpy array X = np.array([1,2,3,-1,-2]) Y = np.array([6,1,-4,2,5]) # Plotting point using scatter method plt.scatter(X,Y) plt.show()

How to plot points in matplotlib with Python

The explanation for the above example is the same as the first example, the only difference is that we stored more than one variable in X and Y, since we have to plot more than one point.

# importing two required module import numpy as np import matplotlib.pyplot as plt # Taking points on x-axis from 0 to 10 and the last argument 30 is stating that 10 is divided into thirty equal interval. x = np.linspace(0,10,30) # y is a sine function y = np.sin(x) # Plotting point using scatter method plt.scatter(x, y,color="black") plt.show()

plot a sine function point in matplotlib python

In the above example again the explanation is the same as explained above, the only difference is that we used a new method “np.linspace(0,10,30)“. This method is used to divide an equal interval between two points.

Let’s understand with an example:-

Let say we used “np.linspace(0,10,30)” this means that we are dividing 0-10 interval into 30 equal interval. So, there are 30 points located in above plot.

You may also read these related articles:-

Источник

Matplotlib Plotting

The plot() function is used to draw points (markers) in a diagram.

By default, the plot() function draws a line from point to point.

The function takes parameters for specifying points in the diagram.

Parameter 1 is an array containing the points on the x-axis.

Parameter 2 is an array containing the points on the y-axis.

If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to the plot function.

Example

Draw a line in a diagram from position (1, 3) to position (8, 10):

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 8])
ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints)
plt.show()

Result:

The x-axis is the horizontal axis.

The y-axis is the vertical axis.

Plotting Without Line

To plot only the markers, you can use shortcut string notation parameter ‘o’, which means ‘rings’.

Example

Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 8])
ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints, ‘o’)
plt.show()

Result:

You will learn more about markers in the next chapter.

Multiple Points

You can plot as many points as you like, just make sure you have the same number of points in both axis.

Example

Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to position (8, 10):

import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 2, 6, 8])
ypoints = np.array([3, 8, 1, 10])

plt.plot(xpoints, ypoints)
plt.show()

Result:

Default X-Points

If we do not specify the points on the x-axis, they will get the default values 0, 1, 2, 3 (etc., depending on the length of the y-points.

So, if we take the same example as above, and leave out the x-points, the diagram will look like this:

Example

import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10, 5, 7])

Result:

The x-points in the example above are [0, 1, 2, 3, 4, 5].

Источник

Читайте также:  Css селектор все соседние
Оцените статью