Code前端首页关于Code前端联系我们

Python 数据可视化教程:创建简单的图形和属性

terry 2年前 (2023-09-25) 阅读数 50 #后端开发

Python 有一个很棒的数据可视化库。 Pandas、numpy 和 matplotlib 的组合可以帮助创建几乎任何类型的可视化。在本章中,我们将开始研究一些简单的图和图的各种属性。

创建图表

这里使用numpy库创建创建图形所需的图形,并使用matplotlib中的pyplot方法绘制实际图形。

import numpy as np 
import matplotlib.pyplot as plt 

x = np.arange(0,10) 
y = x ^ 2 
#Simple Plot
plt.plot(x,y)
print('yes, all jobs done')
Python

运行上面的代码示例并获得如下输出图表 -

Python数据可视化教程:创建简单图表及属性

轴标签

可以使用库中的适当方法将标签应用于轴以及图表标题,如下所示。 ?被展示。

import numpy as np 
import matplotlib.pyplot as plt 

x = np.arange(0,10) 
y = x ^ 2 
#Labeling the Axes and Title
plt.title("Graph Drawing") 
plt.xlabel("Time") 
plt.ylabel("Distance") 

# Formatting the line colors
plt.plot(x,y,'r')

# Formatting the line type  
plt.plot(x,y,'>')
Python

运行上面的示例代码,得到如下输出图形 -

Python数据可视化教程:创建简单图表及属性

保存图形文件

如下所示,可以使用库中适当的方法将图形保存为各种图像文件格式。

import numpy as np 
import matplotlib.pyplot as plt 

x = np.arange(0,10) 
y = x ^ 2 
#Labeling the Axes and Title
plt.title("Graph Drawing") 
plt.xlabel("Time") 
plt.ylabel("Distance") 

# Formatting the line colors
plt.plot(x,y,'r')

# Formatting the line type  
plt.plot(x,y,'>') 

# save in pdf formats
plt.savefig('timevsdist.pdf', format='pdf')
Python

上面的代码在python环境的默认路径下创建了一个pdf文件。

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门