美文网首页大数据 爬虫Python AI SqlPython小哥哥
Python使用matplotlib实现数据可视化教程!

Python使用matplotlib实现数据可视化教程!

作者: 14e61d025165 | 来源:发表于2019-04-11 14:25 被阅读0次

    目前有很多工具都可以数实现据可视化,比如我们常用的Excel,还有数据科学另一门重要的语言R,以及百度的Echarts等可视化工具。今天我们的主角是Python中的matplotlib库(官网: http://matplotlib.org ),相对来说,matplotlib功能不是最强大的,但是对于基本的图形来说,matplotlib已经很不错了。

    首先补充以下:7种颜色 r g b y m c k (红,绿,蓝,黄,品红,青,黑)

    在科研的过程中,坐标系中的XY不一定就是等尺度的。例如在声波中对Y轴取对数。肆意我们也必须知道这种坐标系如何画出来的。

    1,对数坐标图

    有3个函数可以实现这种功能,分别是:semilogx(),semilogy(),loglog()。它们分别表示对X轴,Y轴,XY轴取对数。下面在一个2*2的figure里面来比较这四个子图(还有plot())。

    import numpy as np

    import matplotlib.pyplot as plt

    w=np.linspace(0.1,1000,1000)

    p=np.abs(1/(1+0.1j*w))

    plt.subplot(221)

    plt.plot(w,p,lw=2)

    plt.xlabel('X')

    plt.ylabel('y')

    plt.subplot(222)

    plt.semilogx(w,p,lw=2)

    plt.ylim(0,1.5)

    plt.xlabel('log(X)')

    plt.ylabel('y')

    plt.subplot(223)

    plt.semilogy(w,p,lw=2)

    plt.ylim(0,1.5)

    plt.xlabel('x')

    plt.xlabel('log(y)')

    plt.subplot(224)

    plt.loglog(w,p,lw=2)

    plt.ylim(0,1.5)

    plt.xlabel('log(x)')

    plt.xlabel('log(y)')

    plt.show()

    如上面的代码所示,对一个低通滤波器函数绘图。得到四个不同坐标尺度的图像。如下图所示:

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554963899552" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    在这里还是要推荐下我自己建的Python开发学习群:683380553,群里都是学Python开发的,如果你正在学习Python ,小编欢迎你加入,大家都是软件开发党,不定期分享干货(只有Python软件开发相关的),包括我自己整理的一份2018最新的Python进阶资料和高级开发教程,欢迎进阶中和进想深入Python的小伙伴

    2,极坐标图像

    极坐标系中的点由一个夹角和一段相对于中心位置的距离来表示。其实在plot()函数里面本来就有一个polar的属性,让他为True就行了。下面绘制一个极坐标图像:

    import numpy as np

    import matplotlib.pyplot as plt

    theta=np.arange(0,2*np.pi,0.02)

    plt.subplot(121,polar=True)

    plt.plot(theta,2*np.ones_like(theta),lw=2)

    plt.plot(theta,theta/6,'--',lw=2)

    plt.subplot(122,polar=True)

    plt.plot(theta,np.cos(5*theta),'--',lw=2)

    plt.plot(theta,2np.cos(4theta),lw=2)

    plt.rgrids(np.arange(0.5,2,0.5),angle=45)

    plt.thetagrids([0,45,90])

    plt.show()

    整个代码很好理解,在后面的13,14行没见过。第一个plt.rgrids(np.arange(0.5,2,0.5),angle=45) 表示绘制半径为0.5 1.0 1.5的三个同心圆,同时将这些半径的值标记在45度位置的那个直径上面。plt.thetagrids([0,45,90]) 表示的是在theta为0,45,90度的位置上标记上度数。得到的图像是:

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554963899555" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    3,柱状图:核心代码matplotlib.pyplot.bar(left, height, width=0.8, bottom=None, hold=None, **kwargs)里面重要的参数是左边起点,高度,宽度。下面例子:

    import numpy as np

    import matplotlib.pyplot as plt

    n_groups = 5

    means_men = (20, 35, 30, 35, 27)

    means_women = (25, 32, 34, 20, 25)

    fig, ax = plt.subplots()

    index = np.arange(n_groups)

    bar_width = 0.35

    opacity = 0.4

    rects1 = plt.bar(index, means_men, bar_width,alpha=opacity, color='b',label= 'Men')

    rects2 = plt.bar(index + bar_width, means_women, bar_width,alpha=opacity,color='r',label='Women')

    plt.xlabel('Group')

    plt.ylabel('Scores')

    plt.title('Scores by group and gender')

    plt.xticks(index + bar_width, ('A', 'B', 'C', 'D', 'E'))

    plt.ylim(0,40)

    plt.legend()

    plt.tight_layout()

    plt.show()

    得到的图像是:

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554963899557" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    4,散列图,有离散的点构成的。函数是:matplotlib.pyplot.scatter(x, y, s=20, c='b', marker='o', cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, hold=None,**kwargs),其中,xy是点的坐标,s点的大小,maker是形状可以maker=(5,1)5表示形状是5边型,1表示是星型(0表示多边形,2放射型,3圆形);alpha表示透明度;facecolor=‘none’表示不填充。例子如下:

    import numpy as np

    import matplotlib.pyplot as plt

    plt.figure(figsize=(8,4))

    x=np.random.random(100)

    y=np.random.random(100)

    plt.scatter(x,y,s=x*1000,c='y',marker=(5,1),alpha=0.5,lw=2,facecolors='blue')

    plt.xlim(0,1)

    plt.ylim(0,1)

    plt.show()

    上面代码的facecolors参数使得前面的c=‘y’不起作用了。图像:

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554963899560" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    去掉c=‘y’

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554963899562" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    5,3D图像,主要是调用3D图像库。看下面的例子:

    import numpy as np

    import matplotlib.pyplot as plt

    import mpl_toolkits.mplot3d

    x,y=np.mgrid[-2:2:20j,-2:2:20j]

    z=xnp.exp(-x2-y*2)

    ax=plt.subplot(111,projection='3d')

    ax.plot_surface(x,y,z,rstride=2,cstride=1,cmap=plt.cm.coolwarm,alpha=0.8)

    ax.set_xlabel('x')

    ax.set_ylabel('y')

    ax.set_zlabel('z')

    plt.show()

    得到的图像如下图所示:

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1554963899564" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

    <input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

    相关文章

      网友评论

        本文标题:Python使用matplotlib实现数据可视化教程!

        本文链接:https://www.haomeiwen.com/subject/leutwqtx.html