使用matplotlib绘图的基本知识和技巧有哪些
Admin 2022-08-15 群英技术资讯 490 次浏览
测试环境:
Jupyter QtConsole 4.2.1
Python 3.6.1
1. 基本画线:
以下得出红蓝绿三色的点
import numpy as np
import matplotlib.pyplot as plt
# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)
# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()
以下设置线宽,得到比较粗一点儿的线,如果 plot中只给了一维信息,
默认图形是把数值匹配成纵坐标的
x = np.arange(0., 5., 0.1)
plt.plot(x, 4*x, linewidth=8.0)
plt.show()
以下得到同一个图中两幅分图:
import numpy as np
import matplotlib.pyplot as plt
def f(t):
return np.exp(-t) * np.cos(2*np.pi*t)
t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)
plt.figure(1)
plt.subplot(211)#表示两幅图竖着排列
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')
plt.subplot(212)#如果为(221)和(222)表示横排列
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
plt.show()
2. 画直方图
以下为正态分布
np.random.seed(19680801)
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
# the histogram of the data
n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.1)
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ')
plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()
其中
n, bins, patches = plt.hist(arr, bins=10, normed=0, facecolor='black', edgecolor='black',alpha=1,histtype='bar')
hist的参数非常多,但常用的就这六个,只有第一个是必须的,后面四个可选
arr: 需要计算直方图的一维数组
bins: 直方图的柱数,可选项,默认为10
normed: 是否将得到的直方图向量归一化。默认为0
facecolor: 直方图颜色
edgecolor: 直方图边框颜色
alpha: 透明度
histtype: 直方图类型,‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’
返回值 :
n: 直方图向量,是否归一化由参数normed设定
bins: 返回各个bin的区间范围
patches: 返回每个bin里面包含的数据,是一个list
3. 特殊符号和标注
用以下方式可以写出特殊的数学公式符号:
plt.title(r'$\sigma_i=15$')
以下代码表示文字显示区域是(3, 1.5)指向坐标位置是(2,1)
import numpy as np import matplotlib.pyplot as plt ax = plt.subplot(111) t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), )#shrink表示箭头缩放情况,值越小显示越大
plt.ylim(-2,2)#表示y坐标轴的上界和下界 plt.show()
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
python下如何往数据库批量插入数据?方法是什么?假如我们想要往数据库表中的插入的数据有几百上千条,那么一条条插入,则调用sql语句查询插入就需要执行几百上千,这样花费的时间就非常的长。因此我们可以使用cursor.executemany(sql,args)来实现批量插入数据,那么具体怎样做呢?接着往下看。
这篇文章主要给大家分享的是python中自动化定位元素,那么python中自动化定位元素都有哪些?接下来会给大家介绍到的有find_element_by_id()、find_element_by_name()、find_element_by_class_name()、find_element_by_class_name()等等八种python自动化定位元素的用法,感兴趣的朋友就继续往下看吧。
这篇文章主要介绍了Flask交互基础(GET、 POST 、PUT、 DELETE)的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
相信不少朋友在刷朋友圈时,都会刷到下图所示的九宫格图片,感觉很酷炫的样子,那么这是怎么样做的呢?下面就给就大家分享一下Python生成这样一个九宫格图片的代码。
这篇文章主要介绍了python中的编码和解码及\x和\u问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008