针对数据可视化,对matplotlib
、pygal
这两个包进行简单是使用,尝试利用python绘制折线图、散点图、产生随机漫步数据、模拟投掷骰子并对结果进行统计。
例子来自: 《Python编程从入门到实战》【美】Eric Matthes
安装
如果使用anaconda,则matplotlib
已经安装。若果没有可以使用pip命令安装。这里不做过多赘述。
import matplotlib
测试,如果没有报错,则证明组件包可以正常使用。
使用
plot()
简单使用
import matplotlib.pyplot as plt
squares = [1,4,8,16,25]
plt.plot(squares)
plt.show()
输出图像
修饰图表
import matplotlib.pyplot as plt
squares = [1,4,8,16,25]
plt.plot(squares,linewidth=5) # 设置绘制的线条的粗细
#设置图表标题 坐标轴
plt.title("Square Numbers", fontsize=24) # 图表的标题
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)
# 设置刻度标记的大小
plt.tick_params(axis='both', labelsize=14) #设置刻度样式
plt.show()
输出图像
对应 X Y 坐标
input_values = [1, 2, 3, 4, 5]
squares = [1,4,8,16,25]
plt.plot(input_values,squares,linewidth=5)
plot()函数将接受对用的xy
值用于画图。
scatter() 绘制散点图
import matplotlib.pyplo