1 # 散点图 2 3 import matplotlib.pyplot as plt 4 5 plt.scatter(2, 4) 6 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()
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) #设置图标标题并给坐标轴加上标签 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()
1 import matplotlib.pyplot as plt 2 3 x_values = list(range(1,1001)) 4 y_values = [x **2 for x in x_values] 5 6 plt.scatter(x_values, y_values, s=40) 7 8 #设置图标标题并给坐标轴加上标签 9 plt.title('Square Numbers', fontsize = 24) 10 plt.xlabel('Value', fontsize = 14) 11 plt.ylabel('Square of Value', fontsize = 14) 12 13 #设置刻度标记的大小 14 plt.tick_params(axis='both', which='major', labelsize = 14) 15 16 #设置每个坐标轴的取值范围 17 plt.axis([0, 1100, 0, 1100000]) 18 19 plt.show()
1 import matplotlib.pyplot as plt 2 3 x_values = list(range(1,1001)) 4 y_values = [x **2 for x in x_values] 5 6 plt.scatter(x_values, y_values, edgecolors='None', s=40) 7 8 #设置图标标题并给坐标轴加上标签 9 plt.title('Square Numbers', fontsize = 24) 10 plt.xlabel('Value', fontsize = 14) 11 plt.ylabel('Square of Value', fontsize = 14) 12 13 #设置刻度标记的大小 14 plt.tick_params(axis='both', which='major', labelsize = 14) 15 16 #设置每个坐标轴的取值范围 17 plt.axis([0, 1100, 0, 1100000]) 18 19 plt.show()
1 #自定义颜色 2 import matplotlib.pyplot as plt 3 4 x_values = list(range(1,1001)) 5 y_values = [x **2 for x in x_values] 6 7 plt.scatter(x_values, y_values, c='red', edgecolors='None', s=40) 8 9 #设置图标标题并给坐标轴加上标签 10 plt.title('Square Numbers', fontsize = 24) 11 plt.xlabel('Value', fontsize = 14) 12 plt.ylabel('Square of Value', fontsize = 14) 13 14 #设置刻度标记的大小 15 plt.tick_params(axis='both', which='major', labelsize = 14) 16 17 #设置每个坐标轴的取值范围 18 plt.axis([0, 1100, 0, 1100000]) 19 20 plt.show()
1 # 使用颜色映射(colormap) 2 3 import matplotlib.pyplot as plt 4 5 x_values = list(range(1,1001)) 6 y_values = [x **2 for x in x_values] 7 8 plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, 9 edgecolor='None', s=40) 10 11 #设置图标标题并给坐标轴加上标签 12 plt.title('Square Numbers', fontsize = 24) 13 plt.xlabel('Value', fontsize = 14) 14 plt.ylabel('Square of Value', fontsize = 14) 15 16 #设置刻度标记的大小 17 plt.tick_params(axis='both', which='major', labelsize = 14) 18 19 #设置每个坐标轴的取值范围 20 plt.axis([0, 1100, 0, 1100000]) 21 22 plt.show()
1 # 自动保存图标 2 #将对 plt.show() 的调用替换为对 plt.savefig() 的调用 3 # bbox_inches 对图标多余区域剪裁 4 5 import matplotlib.pyplot as plt 6 7 x_values = list(range(1,1001)) 8 y_values = [x **2 for x in x_values] 9 10 plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, 11 edgecolors='None', s=40) 12 13 #设置图标标题并给坐标轴加上标签 14 plt.title('Square Numbers', fontsize = 24) 15 plt.xlabel('Value', fontsize = 14) 16 plt.ylabel('Square of Value', fontsize = 14) 17 18 #设置刻度标记的大小 19 plt.tick_params(axis='both', which='major', labelsize = 14) 20 21 #设置每个坐标轴的取值范围 22 plt.axis([0, 1100, 0, 1100000]) 23 24 plt.savefig('squares_plot.png', bbox_inches='tight')
上述的代码为matplotlib的基础知识,反正是给自己看的,我也就不特别细心地编辑了,因为刚用博客园写文章,其中的一些编辑功能还不是太熟悉,在教程中还有两个有趣的项目实例:随机漫步和掷骰子,其中掷骰子使用了pygal画廊,使得数据的呈现变得更加直观有趣,具有互动功能,我会各自记录一篇文章。
本人的文档都是自我记录,以便日后查看。
浙公网安备 33010602011771号