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

文章目录

  • 1 概述
  • 2 折线图
  • 3 散点图
  • 4 柱状图
  • 5 拓展

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")

显示的图为

【python】一篇文章入门python简单画图_第1张图片

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("折线图_数据")

显示的图为
【python】一篇文章入门python简单画图_第2张图片

同理我们继续画柱状图,使用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("柱状图_数据")

显示的图为:
【python】一篇文章入门python简单画图_第3张图片

你可能感兴趣的:(python,python,matplotlib,开发语言)