matplotlib中的subplot 练习
来源:互联网 发布:深圳青年旅社知乎 编辑:程序博客网 时间:2024/06/07 07:11
今天用到了matplotlib中的subplot,本来看起来很简单的运用,却也不怎么顺利。
用nodes的数量和概率p产生有向图,我希望自己的结果中能够呈现同一概率,不同nodes的对比,以及同样数量的nodes,不同概率的对比。
遇到的问题如下:
1.怎么通过循环产生子图?
2.子图的标题和坐标轴怎么设置?
3.怎么设置,使子图的坐标使一致的?
4.图的大小怎么设置?
程序源代码
https://github.com/gt11799/graph_ge_by_probability
subplot(ang), ang是个三位数(当然也可以不是)第一位是行数,第二位是列数,第三位(或者三四位)则是第几个子图。
比如
subplot(245)
就表示子图分成2行4列,而现在图绘在第5个位置(从1开始)。subplot赋给变量后,该变量也继承了方法,于是就可以区分开各个子图了。 plot = pyplot.subplot(254) plot.loglog(keys, values, 'o') plot.set_title('p:%s n:%s' %(p/10.0, N))
注意subplot的方法和plot的方法不一样,都是set_,比如设置x轴就是set_xlable(),但是因为图太过于拥挤,我后来给去掉了。把subplot(ang)的ang看作是一个整数,然后每次for循环自加就可以绘出一幅幅子图了。
arrange = 250 for p in range(1, 10, 2): arrange += 1 nor_distri = normalize_distri(graphWithProbability(p/10.0, N)) #generate the graph nor_distri.pop(0, None) keys = nor_distri.keys() values = nor_distri.values() plot = pyplot.subplot(arrange) plot.loglog(keys, values, 'o') plot.set_title('p:%s n:%s' %(p/10.0, N))
现在如果运行程序,就可以看到图像了。但是仍然有不完美的现象。y轴的刻度起始终止并不一致,这在一定程度上限制了对比的效果。subplot有get_ylim()方法可以得到子图的坐标轴的起始终止,然后用set_ylim()方法把其他的子图的坐标轴跟这个统一。如果这个程序要用这个方法,那么就要把第一个子图拿到循环外,或者需要给每一次循环的子图对象命名成不同的变量。都太麻烦,我直接观察了图像,然后直接设置成固定的坐标轴。
plot.set_ylim(0.001, 0.1)
(上面的plot都是subplot的实例)然后又发现图不够大,发现figure()方法可以解决。
pyplot.figure(figsize=(18,7), dpi=80)
figure的大小就变成了18*7个区域,而每个区域的dpi是80。figure还有很多的特性,比如前景背景颜色等等。最后就是要添加图的标题了。注意是suptitle(),如果用tite()会被覆盖的。
pyplot.suptitle('Distribution of in-degree of the graph implemented by probability')
总结起来,整个绘图的程序就是这样的:pyplot.figure(figsize=(18,7))pyplot.suptitle('Distribution of in-degree of the graph implemented by probability')arrange = 250 # for subplotfor p in range(1, 10, 2): arrange += 1 nor_distri = normalize_distri(graphWithProbability(p/10.0, N)) nor_distri.pop(0, None) keys = nor_distri.keys() values = nor_distri.values() plot = pyplot.subplot(arrange) plot.loglog(keys, values, 'o') plot.set_title('p:%s n:%s' %(p/10.0, N)) plot.set_ylim(0.001, 0.1)
生成的图大概是这个样子:
github主页:https://github.com/gt11799
E-mail:gting405@163.com
0 0
- matplotlib中的subplot 练习
- matplotlib.pyplot中的subplot用法
- matplotlib subplot 子图
- matplotlib.pyplot.subplot
- matplotlib常用函数subplot
- matplotlib的subplot使用
- Matplotlib.pyplot中subplot详解
- 【python系列】matplotlib简单画图subplot
- matplotlib注解以及在subplot上绘图
- python matplotlib.subplot绘制子图
- python35 利用matplotlib的subplot画多幅图
- python matplotlib.subplot绘制子图
- python matplotlib.subplot绘制子图
- Matplotlib的子图subplot的使用
- matlab中的subplot函数
- subplot
- subplot
- matplotlib绘制多个子图——subplot
- hdu2159
- C++小细节(不定期整理 )
- IO和NIO的区别
- 嵌入式 FTP断点续传原理以及实现指定下载文件起始地址
- oracle中lpad和rpad函数的使用方法
- matplotlib中的subplot 练习
- 如何学习设计模式
- 操作系统--Linux常用命令(1)
- Android 中的内容观察者ContentObserver
- 基于局域网的扩展认证协议EAPOL Extensible Authentication Protocol
- 深入理解C++的动态绑定和静态绑定
- 宏锦软件 Android 的 ListView 使用详解
- 剑指offer42:翻转单词顺序 VS 左旋转字符串(更高效、简便的解法)
- Android中内容观察者的使用---- ContentObserver类详解