from matplotlib import pyplot as plot
# 折线图
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plot.xticks(range(1, 6)) # 设置x轴刻度
plot.plot(x, y) # x轴, y轴
plot.show()
# plot.savefig("./pic.png")
# 柱状图
x = [1, 2, 3, 4, 5, 6]
y = [6, 5, 4, 3, 2, 1]
plot.bar(x, y)
plot.show()
import pandas
from collections import OrderedDict
import matplotlib.pyplot as plot
exam_dictionary = {
'学习时间': [0.50, 0.75, 1.00, 1.25, 1.50, 1.75, 1.75, 2.00, 2.25, 2.50, 2.75, 3.00, 3.25, 3.50, 4.00, 4.25, 4.50, 4.75, 5.00, 5.50],
'分数': [10, 22, 13, 43, 20, 22, 33, 50, 62, 48, 55, 75, 62, 73, 81, 76, 64, 82, 90, 93]
}
exam_order_dictionary = OrderedDict(exam_dictionary)
"""
学习时间 分数
0 0.50 10
1 0.75 22
2 1.00 13
"""
exam_data_frame = pandas.DataFrame(exam_order_dictionary)
print(exam_data_frame.head(3))
# 取列操作
exam_X = exam_data_frame.loc[:, '学习时间']
exam_y = exam_data_frame.loc[:, '分数']
print(exam_X)
print(exam_y)
# 画散点图
plot.scatter(exam_X, exam_y, color='b', label='exam data')
plot.xlabel('Hours')
plot.ylabel('Score')
plot.show()
import matplotlib.pyplot as plot
import numpy
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plot.plot(x, y)
plot.axis([0, 6, 0, 20]) # x轴坐标:0-6 y轴坐标:0-20
plot.show()
import matplotlib.pyplot as plot
import numpy
"""
同一张图表上的三个函数
"""
t = numpy.arange(0, 5, 0.2)
x_1 = t
y_1 = t
x_2 = t
y_2 = t ** 2
x_3 = t
y_3 = t ** 3
line_list = plot.plot(x_1, y_1, x_2, y_2, x_3, y_3)
plot.setp(line_list, color='blue')
plot.show()
import matplotlib.pyplot as plot
"""
title, x坐标轴, y坐标轴 添加描述
"""
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plot.plot(x, y)
plot.xlabel('x')
plot.ylabel('y')
plot.title('title')
plot.annotate('this is comment', xy=(2, 5), xytext=(2, 10), arrowprops=dict(facecolor='black', shrink=0.1))
plot.show()
import matplotlib.pyplot as plot
"""
画板内画多个子图
"""
fig = plot.figure(1) # 画板
ax_1 = plot.subplot(2, 1, 1) # 2: 布局是两行 1: 布局是一列 1: 这是其中第一个
plot.plot([1, 2, 3])
ax_2 = plot.subplot(2, 1, 2)
plot.plot([4, 5, 6])
plot.show()