首页 > Python资料 博客日记

Python图表属性

2023-07-31 16:20:26Python资料围观192

Python资料网推荐Python图表属性这篇文章给大家,欢迎收藏Python资料网享受知识的乐趣

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')

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

标记轴

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

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") 
#Simple Plot
plt.plot(x,y)

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

格式化线条类型和颜色

图表中线条的样式和颜色可以使用库中适当的方法指定,如下所示。

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,'>')

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

保存图表文件

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

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环境的默认路径中创建pdf文件。


版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

上一篇:Python数据聚合
下一篇:Python图表样式

相关文章

本站推荐