目录

Python Chart Properties

Python具有出色的数据可视化库。 Pandasnumpymatplotlib组合可以帮助创建几乎所有类型的可视化图表。 在本章中,我们将开始查看一些简单的图表和图表的各种属性。

创建图表

我们使用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)

output如下 -

chartprop1.png

对轴进行实验

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

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)

output如下 -

chartprop2.png

格式化线型和颜色

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

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

output如下 -

chartprop3.png

保存图表文件

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

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文件。

↑回到顶部↑
WIKI教程 @2018