Home >Backend Development >Python Tutorial >How to draw a curve graph using python

How to draw a curve graph using python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-11 15:21:5911032browse

How to draw a curve graph using python

How to use python to draw a curve graph? The following are the basic steps:

Prerequisite

First, in order to actually use Matplotlib, we need to install it.

Installation

If you have a later version of Python installed, you should be able to open cmd.exe or terminal and execute:

pip install matplotlib

Note : If the shorter command above doesn't work, you may need to execute C:/Python34/Scripts/pip install matplotlib.

If you receive errors like "No named module" and module name when importing matplotlib, this means you need to install the module as well. A common problem is a missing module named six. This means you need to use pip to install six.

Alternatively, you can go to Matplotlib.org and download the appropriate version to install by visiting the download page. Keep in mind that because your operating system is 64-bit, you don’t necessarily need a 64-bit version of Python. If you're not going to try 64-bit, you can use 32-bit. Open IDLE and read the top. If it says you're 64-bit, you're 64-bit, if it says 32-bit, you're 32-bit. Once you have Python installed, you are ready to write any logic you want. I like using IDLE for programming, but feel free to use whatever you like.

Related recommendations: "python video tutorial"

Step one:

Open the Python shell interface, as shown in the figure shown.

How to draw a curve graph using python


##Step 2:

Enter the following code to import The function library we use.

>>> import numpy as np
>>> import matplotlib.pyplot as plt

The third step:

Generate the data of the function we want to draw, taking the sin function as an example, the code is as follows.

>>> x=np.arange(0,5,0.1);
>>> y=np.sin(x);

Step 4:

Enter the following commands and we can see the function we want to draw.

plt.plot(x,y)

Step 5:

We can see the image we want to draw.

After using the code just now, the picture below may not be displayed. Then enter the following code:

plt.show()

How to draw a curve graph using python

The above is the detailed content of How to draw a curve graph using python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn