来源:《Python编程:从入门到实践》
对于matplotlib的安装可以参照这里
查看使用matplotlib制作的各种example,可访问http://matplotlib.org/
mpl_squares.py
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
plt.plot(squares)
plt.show()
模块pyplot
,pyplot包含很多用于生成图表的函数
plot()——这个函数尝试根据这些数字绘制出有意义的图形
plt.show()打开matplotlib查看器,并显示绘制的图形
mpl_squares.py
import matplotlib.pyplot as plt
squares = [1, 4, 9, 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()
参数linewidth
决定了plot()绘制的线条的粗细函数title()
给图表指定标题参数fontsize
指定了图表中文字的大小函数xlabel()和ylabel()
为每条轴设置标题函数tick_params()
设置刻度的样式,其中指定的实参将影响x和y轴上刻度(axis=‘both’),并将刻度标记的字号设置为14(labelsize=14)向plot()提供一系列数字时,它假设第一个数据点对应的x坐标值为0
,但我们第一个对应的x值为1为改变这种默认行为,给plot()同时提供输入值和输出值
mpl_squares.py
import matplotlib.pyplot as plt
input_values = [1, 2, 3, 4, 5]
squares = [1, 4, 9, 16, 25]
plt.plot(input_values, squares, linewidth=5)
--snip--
scatter_squares.py
import matplotlib.pyplot as plt
plt.scatter(2, 4)
plt.show()
import matplotlib.pyplot as plt
plt.scatter(2, 4, s=200)
# 设置图表标题 & 给坐标轴加上标签
plt.title("Square Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Square of Value", fontsize=14)
# 设置刻度标记的大小
plt.tick_params(axis='both', which='major', labelsize=14)
plt.show()
scatter_squares.py
import matplotlib.pyplot as plt
x_values = [1, 2, 3, 4, 5]
y_values = [1, 4, 9, 16, 25]
plt.scatter(x_values, y_values, s=100)
# 设置图表标题 & 给坐标轴加上标签
--snip--
scatter_squares.py
import matplotlib.pyplot as plt
x_values = list(range(1, 1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values, y_values, s=40)
# 设置图表标题 & 给坐标轴加上标签
--snip--
# 设置每个坐标轴的取值范围
plt.axis([0, 1100, 0, 1100000])
plt.show()
list()
生成一个包含x值(1~1000)的列表列表解析
函数axis()指定了每个坐标轴的取值范围
。它要求提供四个值:x & y轴的最小值和最大值edgecolor='none'
plt.scatter(x_values, y_values, edgecolor='none', s=40)
plt.scatter(x_values, y_values, c='red', edgecolor='none', s=40)
plt.scatter(x_values, y_values, c=(0, 0, 0.8), edgecolor='none', s=40)
scatter_squares.py
import matplotlib.pyplot as plt
x_values = list(range(1, 1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues,
edgecolor='none', s=40)
# 设置图表标题 & 给坐标轴加上标签
--snip--
注意 pyplot所有的颜色映射,可访问http://matplotlib.org,Examples -> Color Examples -> colormaps_reference
# 第一个实参:文件名,然后存储到scatter_squares.py所在的目录中
# 第二个实参:将图表多余的空白区域裁剪掉,
plt.savefig('squares_plot.png', bbox_inches='tight')
15-1 立方:数字的三次方被称为其立方。请绘制一个图形,显示前5个整数的立方值,再绘制一个图形,显示前5000个整数的立方值
1-5整数的立方:
import matplotlib.pyplot as plt
x_values = list(range(1, 6))
y_values = [x**3 for x in x_values]
plt.scatter(x_values, y_values, edgecolor='none', s=40)
# 设置图表标题 & 给坐标轴加上标签
plt.title("Cubic Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Cubic of Value", fontsize=14)
# 设置刻度标记的大小
plt.tick_params(axis='both', which='major', labelsize=14)
plt.show()
5000整数的立方:
import matplotlib.pyplot as plt
x_values = list(range(1, 5001))
y_values = [x**3 for x in x_values]
plt.scatter(x_values, y_values, edgecolor='none', s=40)
# 设置图表标题 & 给坐标轴加上标签
plt.title("Cubic Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Cubic of Value", fontsize=14)
# 设置刻度标记的大小
plt.tick_params(axis='both', which='major', labelsize=14)
# 设置每个坐标轴的取值范围
plt.axis([0, 5100, 0, 5100**3])
plt.show()
15-2 彩色立方:给你前面绘制的立方图指定颜色映射
import matplotlib.pyplot as plt
x_values = list(range(1, 5001))
y_values = [x**3 for x in x_values]
plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Greens,
edgecolor='none', s=40)
# 设置图表标题 & 给坐标轴加上标签
plt.title("Cubic Numbers", fontsize=24)
plt.xlabel("Value", fontsize=14)
plt.ylabel("Cubic of Value", fontsize=14)
# 设置刻度标记的大小
plt.tick_params(axis='both', which='major', labelsize=14)
# 设置每个坐标轴的取值范围
plt.axis([0, 5100, 0, 5100**3])
plt.show()