How to create simple plot using Matplotlib library?

To create graphs in matplotlib is to install the matplotlib library. This library can be installed in different ways, and the simplest method is to type in pip install matplotlib inside Cmd (Windows), or terminal (Linux). If you have installed Python(x,y) or Anaconda distribution of Python then you probably, already have matplotlib library installed. To create the plot using matplotlib library include the library (if installed) in python script by typing:

import matplotlib.pyplot as plt

This part of a library is sufficient for 2D plotting (scatter or simple plot). To graphically represent x and y list on 2D diagram type in the following code:

plt.figure()

plt.plot(x,y)

plt.show()

In previous code the x and y are lists that will be used and graphically represented using matplotlib library. It should be mentioned that x and y list must have the same size (length), otherwise the Python will return error. The minimum working example is given below.

import numpy as np

import matplotlib.pyplot as plt

x=np.arange(0,20.01, 0.01)

y = np.sin(x)

plt.figure()

plt.plot(x,y)

plt.show()

After execution of previous written minimum working example you should get the following diagram.

Figure 1 - Simple plot created as the result of minimum working example 


Nema komentara:

Objavi komentar