- Python数据科学教程
- Python数据科学 - 首页
- Python数据科学 - 入门
- Python数据科学 - 环境设置
- Python数据科学 - Pandas
- Python数据科学 - Numpy
- Python数据科学 - SciPy
- Python数据科学 - Matplotlib
- Python数据处理
- Python数据操作
- Python数据清洗
- Python处理CSV数据
- Python处理JSON数据
- Python处理XLS数据
- Python关系型数据库
- Python NoSQL数据库
- Python日期和时间
- Python数据整理
- Python数据聚合
- Python读取HTML页面
- Python处理非结构化数据
- Python词元化
- Python词干提取和词形还原
- Python数据可视化
- Python图表属性
- Python图表样式
- Python箱线图
- Python热力图
- Python散点图
- Python气泡图
- Python 3D图表
- Python时间序列
- 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)
其输出如下:
轴标签
我们可以使用库中的适当方法为轴添加标签,并为图表添加标题,如下所示。
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文件。
广告