美文网首页
matplot学习

matplot学习

作者: 倔犟的贝壳 | 来源:发表于2019-05-26 11:23 被阅读0次

matplot是一款python的一个绘图库。在机器学习过程中,我们可以用它绘制损失函数图,可以使用它将数据可视化等等。以下是一些基本图形的绘制的总结。

折线图

import matplotlib.pyplot as plt
import numpy as np
x = np.array([1,5,10,12,15,18,20,22,26])
y = np.array([50,100,120,140,150,170,172,174,174])

#plt.plot(x, y,'g',linewidth=2,linestyle='--') #虚线
plt.plot(x, y,'b',linewidth=2,linestyle='-') #实线
plt.show()
output_2_0.png

函数图

import matplotlib.pyplot as plt
import numpy as np 

def function():
    x = np.linspace(0,2*np.pi,50)

    siny = np.sin(x)
    cosy =np.cos(x)
    y = 0*x
    plt.plot(x,siny,label = "sin")
    plt.plot(x,cosy,'r',label = 'cos')
    plt.plot(x,y,'g')
    
    #设置说明框
    legend = plt.legend(loc='best', shadow=True)
    frame = legend.get_frame()
    frame.set_facecolor('0.90') #frame的颜色
    
    plt.show()
    
function()
    
output_4_0.png

柱状图

def barPicture() :
    #简单画法
    x = np.array(range(1,13))
    y = np.array([2,5,15,18,20,27,30,31,32,25,17,10])
    plt.bar(x,y,0.8, color = 'y')
    plt.xlabel("month")
    plt.ylabel("temp")
    plt.ylim(0,38)
    for x1, y1 in zip(x,y):
        plt.text(x1,y1+0.1,'%.2f' %y1, ha = 'center',va = "bottom")
    plt.show()
barPicture()
output_6_0.png

直方图

    p = np.random.rand(1000)
    plt.hist(p,bins=30,color='g',edgecolor='k')
    plt.show()
output_8_0.png

散点图

import numpy as np
import matplotlib.pyplot as plt

x = np.random.normal(0,1.0,1024)
y = np.random.normal(0,1.0,1024)
print(x)
print(y)
color = np.arctan2(y, x)
plt.scatter(x, y, s=30, alpha=0.8, c=color)  #s 表示圆点的大小
# 设置坐标轴范围
plt.xlim((-1.5, 1.5))
plt.ylim((-1.5, 1.5))

# 不显示坐标轴的值
plt.xticks(())
plt.yticks(())
plt.show()
[-0.02492004  1.1578533  -1.11873907 ...  0.35172424  0.12058419
 -1.23191611]
[ 0.76621221  0.3721741  -0.13165951 ... -0.5528842   1.8072641
 -0.03400544]
output_10_1.png

等高线

x = np.linspace(-3, 3, 256)
y = np.linspace(-3, 3, 256)

# 生成网格线
X, Y = np.meshgrid(x, y)
Z =X**2 + Y**2
plt.contourf(X, Y, Z, levels=8, alpha=0.75, cmap=plt.cm.gist_earth)
# 绘制等高线
C = plt.contour(X, Y, Z, 8, colors='red')
# 添加数值
plt.clabel(C, inline=True, fontsize=10)
plt.show()
output_14_0.png

3D立体图

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = np.arange(-5, 5, 0.25)
y = np.arange(-5, 5, 0.25)

X, Y = np.meshgrid(x, y)

# 计算每个点对的长度
R = np.sqrt(X**2+Y**2)
# 计算Z轴的高度
Z = np.sin(R)

fig = plt.figure()
ax = Axes3D(fig)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))


ax.contour(X,Y,Z,zdim = 'z',offset=-2,cmp = "rainbow")
ax.set_zlim(-2,2)
plt.show()
output_16_0.png

相关文章

  • matplot学习

    matplot是一款python的一个绘图库。在机器学习过程中,我们可以用它绘制损失函数图,可以使用它将数据可视化...

  • matplot学习:常用函数

    add_axes(*args, **kwargs) maplot.figure.Figure.add_axes(P...

  • python matplot显示os task调度--Apple

    一,前言 python matplot绘制甘特图--Apple的学习笔记[https://www.jianshu....

  • matplot

    01 代码下载 02 代码下载

  • matplot

    柱形图主要用于比较各组数据之间的差别或数据变化情况。折线图趋势分析饼图主要用于各部分占整体的多少说明。散点图查找变...

  • 笔记|数据分析之pandas基础----matplotlib基础

    matplot API入门 如何引入: 一个简单的例子: 效果如下: Figure和Subplot matplot...

  • python下matplotlib 绘图入门

    matplotlib 本文是在ipython notebook上编写,是matplot的学习笔记 对一些常用的图形...

  • Matplot入门

    开始 画图是很细节的事情,matplotlib的入门,从试过每个参数开始。以下练手,参考的 https://www...

  • 安装matplot

     为了安装matplot,需要升级pip,需要安装python大于3.5的版本。  弄完了,执行上述命令,提示我:...

  • TrendVis

    TrendVis TrendVis is a plotting package that uses matplot...

网友评论

      本文标题:matplot学习

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