python绘制图形_如何使用python绘制图形

本文介绍了如何使用Python进行图形绘制,详细讲解了利用Python进行数据可视化的基础步骤,适用于初学者。
摘要由CSDN通过智能技术生成

python绘制图形

Many Data scientist prefer using Python to plot a graph to visualize their data. Creating a graph using Python is possible due to its efficient libraries. Python libraries like Pandas, Numpy and Matplotlib are used vastly to analyze the data using python. To plot a graph using Python we are going to use Matplotlib library.

许多数据科学家更喜欢使用Python绘制图形以可视化其数据。 由于其高效的库,因此可以使用Python创建图形。 诸如Pandas,Numpy和Matplotlib之类的Python库被大量用于使用python分析数据。 为了使用Python绘制图形,我们将使用Matplotlib库。

I would suggest you to try Google Colab or Jupyter Notebook to create graphs for your data, as installing the libraries in Python may not support some devices. I personally use Google Colab to visualize my datasets, as it is faster and it doesn’t require any installation of software on all devices that you use. You need only a browser to work with Google Colab.

我建议您尝试使用Google ColabJupyter Notebook为数据创建图形,因为在Python中安装库可能不支持某些设备。 我个人使用Google Colab来可视化我的数据集,因为它速度更快并且不需要在您使用的所有设备上安装任何软件。 您只需要一个浏览器即可使用Google Colab。

1.导入Matplotlib库 (1. Import Matplotlib library)

To start with creating graphs in Python we require Matplotlib library which supports plotting of various graphs using Python. It is very easy to import libraries in Python, just type the following command and click run.

要开始使用Python创建图形,我们需要Matplotlib库,该库支持使用Python绘制各种图形。 使用Python导入库非常容易,只需键入以下命令,然后单击运行。

import matplotlib.pyplot as plt

Basically, it tells Python that we need the Matplotlib library to create a graph. Therefore, Python itself imports the library to that program. Pyplot is the function in Matplotlib which is specifically used to create charts. It is not convenient to type matplotlib.pyplot every time we require it in the program, hence we imported it as plt. So, whenever matplotlib.pyplot is required in a program we can simply use plt.

基本上,它告诉Python我们需要Matplotlib库来创建图形。 因此,Python本身将库导入到该程序。 Pyplot是Matplotlib中的函数,专门用于创建图表。 每次在程序中需要它时都不方便键入matplotlib.pyplot,因此我们将其导入为plt。 因此,只要程序中需要matplotlib.pyplot,我们都可以简单地使用plt。

2.编写您的数据 (2. Write Your Data)

Most of the Data scientist work with huge datasets which are stored in excel or text file. But, we are going to work with a small set of data. So we can just type or copy the data in Python. For your understanding, let's consider the following values of x-axis and y-axis.

大多数数据科学家都使用海量数据集,这些数据集存储在excel或文本文件中。 但是,我们将使用少量数据。 因此,我们只需在Python中输入或复制数据即可。 为了您的理解,让我们考虑以下x轴和y轴的值。

X = [2,5,7,8]
Y = [3,4,1,6]

It creates a list in Python. Basically, it stores values in X and Y. It is simple to work with such a small set of values for understanding purpose. So, we are going to plot a graph which has specific values on the x-axis and specific values on the y-axis. After typing your data in python just click run to store the values in X and Y. X contains the values of x-axis and Y contains the values of the y-axis.

它在Python中创建一个列表。 基本上,它将值存储在X和Y中。为了理解目的,使用如此少的一组值很简单。 因此,我们将绘制一个图,该图在x轴上具有特定值,在y轴上具有特定值。 在python中输入数据后,只需点击运行即可将值存储在X和Y中。X包含x轴的值,Y包含y轴的值。

3.绘制图表 (3. Plot a graph)

To plot a simple line graph in Python just use the library function that we imported earlier. Simply type the below command and run the program to see the line graph.

要在Python中绘制简单的折线图,只需使用我们之前导入的库函数。 只需键入以下命令并运行程序即可查看折线图。

plt.plot(X,Y)
plt.show()

Python plots the line graph for values of X and Y which we typed earlier. We use command plt.show() to display our graph. After running the program you will be able to see a graph like this.

Python绘制了折线图,以绘制我们先前键入的X和Y值。 我们使用命令plt.show()来显示图形。 运行该程序后,您将能够看到这样的图形。

Image for post
Image by the author.
图片由作者提供。

Basically, Python plotted the values of X=2,5,7,8 on x-axis and values of Y=3,4,1,6 on the y-axis. Congratulations on successfully creating a graph for your data using Python. We can also modify the graph for our requirements. We can add a Title to our graph and also label x-axis and y-axis with the help of below command.

基本上,Python在x轴上绘制了X = 2,5,7,8的值,在y轴上绘制了Y = 3,4,1,6的值。 祝贺您使用Python成功为数据创建了图形。 我们还可以根据需要修改图形。 我们可以在图表中添加标题,并借助以下命令标记x轴和y轴。

plt.xlabel('Values of X')
plt.ylabel('Values of Y')
plt.title('Simple Line Graph using Python')
plt.show()

Therefore, Python labels x-axis as Values of X and y-axis as Values of Y. After running the following command you will be able to see a graph like this.

因此,Python将x轴标记为X的值,将y轴标记为Y的值。运行以下命令后,您将能够看到这样的图形。

Image for post
Image by the author
图片由作者提供

Python supports all types of graphs with its Matplotlib library function Pyplot. For me, visualizing data with the help of Python is much easier than any other software. You can always refer to the official documentation of Matplotlib to expand your knowledge regarding this topic.

Python的Matplotlib库函数Pyplot支持所有类型的图。 对我来说,借助Python可视化数据比任何其他软件都容易得多。 您可以始终参考Matplotlib的官方文档来扩展有关该主题的知识。

I would also suggest you create Google Colab notebook to run codes. As you don’t need to install any software on your GPU itself. Try experimenting with your data, try plotting bar charts and histograms using Python to get a deep knowledge regarding visualizing the data using python. You can also refer below YouTube video to get additional information about plotting graphs using Matplotlib.

我也建议您创建Google Colab 笔记本运行代码。 由于您不需要在GPU本身上安装任何软件。 尝试试验数据,尝试使用Python绘制条形图和直方图,以获取有关使用python可视化数据的深入知识。 您还可以参考下面的YouTube视频,以获取有关使用Matplotlib绘制图形的更多信息。

If you want sample codes to practice plotting different graphs using Python you can refer geeks for geeks page about visualizing the data using Python. Best of luck in plotting graphs and visualizing different data.

如果您希望示例代码练习使用Python绘制不同的图形,则可以参考geeks的geeks页面,了解如何使用Python可视化数据。 绘制图表和可视化不同数据的运气最好。

翻译自: https://medium.com/dev-genius/how-to-plot-a-graph-using-python-1ae3a150d014

python绘制图形

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值