Introduction to Python data analysis (1) Simple drawing of images using matpiotlib library

1. Draw a simple line graph

import matplotlib.pyplot as plt
a = [1, 2, 3, 4, 5, 6]
b = [1, 2, 3, 4, 5, 6]
plt.plot(a, b, color = 'yellow', marker = 'o', linestyle = 'solid')
plt.title("title")
plt.xlabel("x")
plt.ylabel("y")
plt.grid()#绘制方格
plt.show()

Drawing effect:

2. Draw a bar graph

import matplotlib.pyplot as plt
a = [1, 2, 3, 4, 5, 6]
b = [1, 2, 3, 4, 5, 6]
a_label = ['1', '2', '3', '4', '5', '6']
plt.xticks(a, a_label)#绘制x轴标签
plt.bar(a, b, width= 0.5, bottom= 1, color = 'green')
plt.show()
#width:单个直方图的宽度, bottom:y轴的起点

drawing effect

 3. Draw complex line graphs

import matplotlib.pyplot as plt
import numpy as np
a = [1, 2, 4, 6, 8, 10, 12, 24]
b = [1, 2, 4, 8, 16, 32, 64, 128]
c = [128, 64, 32, 16, 8, 4, 2, 1]
d = np.random.randint(1, 100, 8)#随机生成8个
x = [i for i in range(0, 8)]
plt.plot(x, a, linestyle = '-', label = 'a')#实线
plt.plot(x, b, linestyle = '-.', label = 'b')#点虚线
plt.plot(x, c, linestyle = ':', label = 'c')#点线
plt.plot(x, d, linestyle = '--', label = 'd')#虚线
plt.legend()#设置图例
plt.title('title')
plt.show()

drawing effect

4. Draw a scatter plot

import matplotlib.pyplot as plt
a = [1, 2, 4, 6, 8, 10, 12, 24]
b = [1, 2, 4, 8, 16, 32, 64, 128]
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
plt.scatter(a, b)
for a, b, labels in zip(a, b, labels):
    plt.annotate(labels, xy = (a, b), xytext = (-5, 5), textcoords = 'offset points', color = 'm')
# plt.annotate用于标注文字
# plt.annotate(s='str', #s为标注的文本内容
# 	xy=(x,y) ,          #xy为被标注的坐标点
# 	xytext=(l1,l2) ,    #xytext为被标注文字的坐标位置
# 	...
# )
plt.title('title')
plt.show()

drawing effect

Guess you like

Origin blog.csdn.net/m0_62577716/article/details/128501404
Recommended