本文主要是介绍画图从零开始,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
2018/03/17
时隔一年又重新开始捡起来机器学习的东西,还是从最简单的画图开始吧。
当然数据集还是从iris开始。
还是在服务器上运行,这部分还是使用原来的那个命令,引入后执行use('Agg')
保证可以保存。
从官方文档处学习:http://scikit-learn.org/stable/auto_examples/datasets/plot_iris_dataset.html#sphx-glr-auto-examples-datasets-plot-iris-dataset-py
version_0:最简单的版本,加载数据后,直接使用plt的默认画图。
1 #! /bin/python2 #coding:utf-83 #2018/03/174 #Author:VChao5 6 import matplotlib7 matplotlib.use('Agg')8 9 10 import matplotlib.pyplot as plt11 from sklearn import datasets12 13 14 def main():15 iris = datasets.load_iris()16 x = iris.data[:,:2]17 y = iris.target18 19 plt.scatter(x[:,0],x[:,1],c = y) 20 plt.savefig("start1.png")21 22 23 24 if __name__ == "__main__":25 main()
途中的标号是从vim里面直接带过来的,带着也没什么不好看,就呆着吧。
结果如图:
缺点:没有标题,没有各个轴的名称
version_1:增加标题,和轴名称
因为这个测试对相应轴的数量值并不敏感,想办法去掉。
version_2:简单修改坐标轴样式
最终版本代码:
1 #! /bin/python2 #coding:utf-83 #2018/03/174 #Author:VChao5 6 import matplotlib7 matplotlib.use('Agg')8 9 10 import matplotlib.pyplot as plt11 from sklearn import datasets12 13 14 def main():15 iris = datasets.load_iris()16 x = iris.data[:,:2]17 y = iris.target18 19 plt.scatter(x[:,0],x[:,1],c = y)20 21 22 plt.xlabel("Sepal length")23 plt.ylabel("Sepal width")24 plt.title("Hello")25 26 #Make the x line like27 plt.xticks(())28 plt.yticks(())29 30 31 32 33 plt.savefig("start1.png")34 35 36 37 38 if __name__ == "__main__":39 main()40
对于个这个点的样式还是可以修改的。后续一点一点修改。
现在单个图的样式已经完成了,但是如果去构造多个图呢,不能说一个一个脚本去写对吧,肯定是从一个图里面,看到多个属性。
version_3:多个图绘制在一起。这里一共是四个属性。正常应该是6个图
如果是说你一个一个的去画,这个工作实在是太繁琐了。当然可能说,以后遇见的数据比较奇葩,各个图本身就不是一样的东西,那就每个单独去画。这里东西还是比较好的,那就最好是函数式编程。
http://blog.topspeedsnail.com/archives/760
上面这个网址是一些子图的绘画方式,这个子图是用subplot
,暂且不去关注到底是什么样的方式,先学会一种再说。
http://blog.csdn.net/cdqn10086/article/details/72403899图片大小,设置的时候,一定要在最开始初始化得时候就去弄,你的其他命令可能是新建了另一个画图。比如错误:
plt.figure(figsize=(10,10))
fig,ax = plt.subplots(nrows=2,ncols=3)
https://www.cnblogs.com/nju2014/p/5620776.html这里具体讲述了这个关系。
这个过程应该说还是挺复杂的,不过最后这个图还算是可以接受。
1 #! /bin/python2 #coding:utf-83 #2018/03/174 #Author:VChao5 6 import matplotlib7 matplotlib.use('Agg')8 9 10 import matplotlib.pyplot as plt11 from sklearn import datasets12 13 14 att_array = [[0,1],[0,2],[0,3],15 [1,2],[1,3],[2,3]]16 17 fig,ax = plt.subplots(figsize=(10,7.5),nrows=2,ncols=3)18 19 def scatter_plt(i,x,y,t,one_ax,x_comment,y_comment):20 #row X col -> sub figure nums21 #i -> the num of nums,cordinate with the plt.subplot22 #x,y,t -> x axis ,y axis data ,t target23 24 #plt.figure(6 -i,figsize=(8,6))25 #plt.clf()26 one_ax.scatter(x,y,c=t)27 one_ax.set_xticks(())28 one_ax.set_yticks(())29 one_ax.set_xlabel(x_comment)30 one_ax.set_ylabel(y_comment) 31 32 33 def main():34 iris = datasets.load_iris()35 36 for i in range(len(att_array)):37 pass38 x = i % 2;39 y = i % 3;40 x_comment = iris.feature_names[att_array[i][0]]41 y_comment = iris.feature_names[att_array[i][1]]42 scatter_plt(i,iris.data[:,att_array[i][0]],43 iris.data[:,att_array[i][1]],44 iris.target,ax[x][y],x_comment,y_comment)45 47 plt.savefig("mul.png")48 52 if __name__ == "__main__":53 main()
效果图:
这里还差一点,图例。不过这里要明白一件事,就是我这个图例应该是所有子图都通用的,那么就应该是在figure这个对象上。初步理解,
不过查了挺多资料以后感觉,我这个环境下下面,添加图例有点小困难。
他们网上都是每次都是不一样的画图然后得到的这个不同的图例,而我是直接利用他自己的参数直接穿进去不同的东西了。也就是这个得到标签的中间代码没有写。
图例的位置可以继续修改,这里就不进行修改了。
1 #! /bin/python2 #coding:utf-83 #2018/03/174 #Author:VChao5 6 import matplotlib7 matplotlib.use('Agg')8 9 import matplotlib.pyplot as plt10 from sklearn import datasets11 12 13 att_array = [[0,1],[0,2],[0,3],14 [1,2],[1,3],[2,3]]15 16 fig,ax = plt.subplots(figsize=(10,7.5),nrows=2,ncols=3)17 18 tmp1 = None19 tmp2 = None20 tmp3 = None21 22 def scatter_plt(i,x,y,t,one_ax,x_comment,y_comment):23 #row X col -> sub figure nums24 #i -> the num of nums,cordinate with the plt.subplot25 #x,y,t -> x axis ,y axis data ,t target26 27 #plt.figure(6 -i,figsize=(8,6))28 #plt.clf()29 global tmp1,tmp2,tmp330 tmp1 = one_ax.scatter(x[:50],y[:50],c="red",label="so")31 tmp2 = one_ax.scatter(x[50:100],y[50:100],c="blue",label="xx")32 tmp3 = one_ax.scatter(x[100:150],y[100:150],c="green",label="xx")33 34 one_ax.set_xticks(())35 one_ax.set_yticks(())36 one_ax.set_xlabel(x_comment)37 one_ax.set_ylabel(y_comment)38 39 def main():40 iris = datasets.load_iris()41 42 for i in range(len(att_array)):43 pass 44 x = i % 2;45 y = i % 3;46 x_comment = iris.feature_names[att_array[i][0]]47 y_comment = iris.feature_names[att_array[i][1]]48 scatter_plt(i,iris.data[:,att_array[i][0]],49 iris.data[:,att_array[i][1]],50 iris.target,ax[x][y],x_comment,y_comment)51 52 fig.legend((tmp1,tmp2,tmp3),["F","Y","X"],loc=2)53 plt.savefig("mul.png")54 55 if __name__ == "__main__":56 main()57
不过这样的代码感觉挺难看的。!!,不如一开始的那个简洁明了。
可能还有别的办法去获取。
对于画图就先到这个底部,后续再继续进行不足。其实后续大框架已经打好了,只需要配置到相应的图像就可以了。
2018/03/18
那本书《python机器学习系统设计》吧,好像是,比我处理的这个过程更好,主要他把数组处理的过程更加简化了。!!
后续再进行添加。
2018/03/24
对于多附图的代码,通过动态添加的方式会比上面这个更好。
https://www.jianshu.com/p/3edb07f58ecd
这篇关于画图从零开始的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!