python使用matplotlib绘制简单图形 您所在的位置:网站首页 python怎么用matplotlib生成图表 python使用matplotlib绘制简单图形

python使用matplotlib绘制简单图形

#python使用matplotlib绘制简单图形| 来源: 网络整理| 查看: 265

1,绘制一个圆

import matplotlib.pyplot as plt

def creat_circle():

    circle=plt.Circle((0,0),radius=0.5,fc='y',ec='r')#圆心坐标,半径,内部及边缘填充颜色

    return circle

def show_shape(path):

    ax=plt.gca() #获取当前对象

    ax.add_patch(path) #给对象传递添加的块

    ax.set_aspect('equal') #设置图形的宽高比,equal为1:1

    plt.axis('scaled') #设置自动调节数轴范围

    plt.show()

c=creat_circle()

show_shape(c)

运行结果如图

2,绘制动图

from matplotlib import pyplot as plt

from matplotlib import animation

def creat_circle():

    circle=plt.Circle((0,0),radius=0.05,fc='y',ec='r')#圆心坐标,半径,内部及边缘填充颜色

    return circle

def update_radius(i,circle):#调用时自动传递的帧编号,每一帧需要的对象;返回也是一个帧对象

    circle.radius=i*0.5

    return circle

def creat_animation():

    fig=plt.gcf() #获取当前Figure对象的引用

    ax=plt.axes()#创建坐标系并设定取值范围

    plt.xlim(-10,10),plt.ylim(-10,10)

    ax.set_aspect('equal')

    circle=creat_circle()

    ax.add_patch(circle) #将圆添加到当前坐标系下

    anim=animation.FuncAnimation(

        fig,update_radius,fargs=(circle,),frames=30,interval=50)#传给FunAnimatin绘制动图的参数

    #依次为:当前Figure对象,绘制每帧的函数,给前面的函数通过元组传递参数(帧数不用传递)

    #帧数(这里选30帧),两帧之间的时间间隔(这里是50毫秒)

   

    plt.title('Dynamic circle')#设置标题

    plt.show() #显示动图

    anim.save('circle.gif',writer='imagemagick')#在当前目录下保存动图

creat_animation()

运行结果

3,绘制抛物线动图:

from matplotlib import pyplot as plt

from matplotlib import animation

import math

g=9.8

def get_intervals(u,theta): #u为初合速度,theta为合速度与地面夹角

    t_flight=2*u*math.sin(theta)/g

    #由抛物线公式得:总飞行时间为两倍的竖直方向速度除竖直加速度g

    intervals=[]

    start=0

    interval=0.005

    #以0.5为间隔创建列表,以便于后续帧与帧之间代表物体运动的时间间隔为0.5

    while start



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有