【python】一篇文章入门python简单画图

1 概述

python 中的画图是直接使用matplotlib,用起来非常简单。

先随机生成相关的数据若干个,用于画图

import random;

def get_random():
    num = random.randint(0,100) #随机生成0~100的数字
    return num

def main():
    list1 = []					#创建空的list用来存储数据
    list2 = []					#创建空的list用来存储数据

    for  i in range(10):
        num = get_random()
        list1.append(num)
        num = get_random()
        list2.append(num)
    print(list1)
    print(list2)


if __name__ == '__main__':
    main()

2 折线图

直接写个画图函数

def plotLineChart(list):
    plt.plot(list)
plt.title("plot") 	#图片的title
plt.show() 			#画图显示,可以多个曲线图画在同一个画布上

完整的代码如下:

import random;

import matplotlib.pyplot as plt

def get_random():
    num = random.randint(0,100) #随机生成0~100的数字
    return num

def plotLineChart(list):
    plt.plot(list)

def main():
    list1 = []
    list2 = []

    for  i in range(10):
        num = get_random()
        list1.append(num)
        num = get_random()
        list2.append(num)
    print(list1)
    print(list2)
    plotLineChart(list1)
    plotLineChart(list2)
    plt.show()

if __name__ == '__main__':
    main()

但是这样画出来的图,你会发现原点的坐标对不上。

我们可以把画图的函数稍微修改成如下的.因为rang函数是左闭右开,所以需要+1

def plotLineChart(list):
    plt.plot(range(1,len(list)+1),list)

显示的图如下:同时也加了大小限制

3 散点图

随机生成散点的数据

def scatterPoint():
    list1 = []
    for  i in range(get_random()):
        x = get_random()
        y = get_random()
        point = [x,y]
        list1.append(point)
    # print(list1)
    return list1

画散点图主要使用的scatter函数。

def plotTest():
    plt.title("plot")
    list = scatterPoint()
    listX = []
    listY = []
    for i in range(len(list)):
        temp = list[i]
        x = temp[0]
        y = temp[1]
        print("x = ",x ,"y = ",y)
        listX.append(x)
        listY.append(y)
    plt.scatter(listX, listY)
    plt.savefig("散点图.png")

4 柱状图

plt.bar(range(10),list1,color='g')   #以范围0~9画x轴,列表list1画y轴

完整的画图函数如下。但是没有显示对应的数据坐标。

def plotTest():
    list1 =[]
    len = 10
    for i in range(len):
        num = get_random()
        print("num = ",num)
        list1.append(num)
    plt.bar(range(len), list1,width=0.2)
    plt.xlabel('x')  # 添加横轴标签\n",
    plt.ylabel('y')  # 添加y轴名称\n",
    plt.savefig("柱状图.png")

显示的图为

在这里插入图片描述

5 拓展

以上的几种画图只能显示整个图形的相关趋势,但是没有标注具体的数据,并且也只是单个图。接下来对这几个图进行拓展。

首先对折线图上的点进行数据标注。

我们需要借用plt.text这个函数。该函数原型如下:需要明确知道的是x,y两个轴的坐标.s 是你想要标注的数据。

def text(x, y, s, fontdict=None, **kwargs):
    return gca().text(x, y, s, fontdict=fontdict, **kwargs)

部分的代码如下:

def plotLineChart(list,name):
    plt.plot(range(1,len(list)+1),list,label = name)

def plotTest():
    list1 = []
    list2 = []

    for  i in range(10):
        num = get_random()
        list1.append(num)
        num = get_random()
        list2.append(num)
    #print(list1)
    #print(list2)
    plotLineChart(list1,"list1")
    plotLineChart(list2,"list2")
    for x,y1,y2 in zip(range(10),list1,list2):
        plt.text(x + 1, y1, y1)
        plt.text(x + 1, y2, y2)
    plt.grid(True, axis='x')   #显示x轴网格线
    plt.grid(True, axis='y')   #显示y轴网格线
    plt.legend()  # lower   right
    plt.title("Line chart")
    plt.savefig("折线图_数据")

显示的图为
在这里插入图片描述

同理我们继续画柱状图,使用plt.text,修改如下:

def plotTest():
    list1 = []
    list2 = []
    len = 10
    x = np.arange(len)
    width = 0.2
    for i in range(len):
        num = get_random()
        #print("num = ",num)
        list1.append(num)
        num = get_random()
        list2.append(num)
        plt.text(i,list1[i],list1[i])
        plt.text(i+width, list2[i], list2[i])

    plt.bar(x, list1,width)
    plt.bar(width+x, list2, width)
    plt.xlabel('x')  # 添加横轴标签\n",
    plt.ylabel('y')  # 添加y轴名称\n",
    plt.savefig("柱状图_数据")

显示的图为:
在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高启强不卖鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值