数据分析与机器学习三:matplotlib--plot折线图


 生成矩阵数据:

import pandas as pd
data = pd.read_csv('./shiyelv.csv')
data['date'] = pd.to_datetime(data['date'])
print(data.head(5))
        date  shiyelv
0 2017-01-01      4.5
1 2017-02-01      4.6
2 2017-03-01      5.0
3 2017-04-01      4.1
4 2017-05-01      3.8

一、拆线图

画一个plot折线图:plt.plot(x轴数据,y轴数据)

import matplotlib.pyplot as plt
plt.plot()  # 画一个折线图,如果plot方法中没有任何数据,则为空图
plt.show()   # 显示图
plt.plot(data['date'], data['shiyelv'])
plt.show()

 

 

1.x轴刻度值的倾斜度:plt.xticks(rotation=多少倾斜度)

plt.plot(data['date'], data['shiyelv'])
plt.xticks(rotation=45) # x轴的刻度数值,45度倾斜
plt.show()

 2.x轴名称:plt.xlabel(轴名称)

3.画图标题:plt.title(图表名称)

plt.plot(data['date'], data['shiyelv'])
plt.xticks(rotation=90)  
plt.xlabel('Month')   # x轴的标题
plt.ylabel('shiyelv')  # y轴的标题
plt.title('monthly shiyelv')  # 画图的标题
plt.show()

 

 

 二、在一个画图区间,画多个plot折线子图

1.

定义画布:figure = plt.figure()

定义或添加子图:ax = figure.add_subplot(横向子图数量,纵向子图数量,这是第几个子图;从0开始)

注意:没有定义或添加过的子图,不会显示

fig = plt.figure()  # figure 就是指定画图的区间
ax1 = fig.add_subplot(2, 2, 1) # 在画图区间内,定义2行2列plot子图中的第1个子图
ax2 = fig.add_subplot(2, 2, 2) # 定义2行2列plot子图中的第2个子图
ax4 = fig.add_subplot(2, 2, 4) # 定义2行2列plot子图中的第4个子图
# 没有定义第3个子图,则第3个子图就不会显示出来
plt.show()

 2.

定义画图区域,并指定区域大小: figure = plt.figure(figsize=(长度,宽度))

为子图添加数据:ax.plot(x轴数据,y轴数据)

fig = plt.figure(figsize=(10, 6)) # 指明画图区间的大小
ax1 = fig.add_subplot(2, 1, 1) 
ax2 = fig.add_subplot(2, 1, 2)
ax1.plot(np.random.randint(1, 5, 5), np.arange(5)) # 为第1个子图添加数据
ax2.plot(np.arange(10)*3, np.arange(10)) # 为第2个子图添加数据
plt.show()

 

 

3.在同一个子图中,画出多条线:ax1.plot多次或者plt.plot多次

ax1.plot(x同数据,y轴数据,color=颜色)

ax1.plot(x同数据,y轴数据,color=颜色)

fig = plt.figure(figsize=(10, 6))
ax1 = fig.add_subplot(2, 1, 1) 
ax2 = fig.add_subplot(2, 1, 2)
ax1.plot(np.random.randint(1, 5, 5), np.arange(5), c='red') # 为折线plot指明颜色color 
ax1.plot(np.random.randint(1, 50, 5), np.arange(5), c='blue') # 为子图添加第二条折线plot
ax2.plot(np.arange(10)*3, np.arange(10)) 
plt.show()
# 颜色也可以用(R, G, B)三元组来表示

 

 4.

为多条折线图,同时添加label:plt.plot(x轴数据,y轴数据,c=颜色,label=此折线的名称)

指定label展示的位置:plt.legend(loc=位置),位置包括

            'best'            0
            'upper right'     1
            'upper left'      2
            'lower left'      3
            'lower right'     4
            'right'           5
            'center left'     6
            'center right'    7
            'lower center'    8
            'upper center'    9
            'center'          10

 

fig = plt.figure(figsize=(10, 6))
colors = ['red', 'blue', 'green', 'orange', 'black']
for i in range(5):
    start_index = i*12
    end_index = (i+1)*12
    subset = data[start_index: end_index]
    label = str(2017 + i)
    plt.plot(subset['date'], subset['shiyelv'], c=colors[i], label=label)  # 为折线添加label
plt.legend(loc='best')  # 指明label展示的地方

 

三、一个比较完整的折线图:

data仍然为失业率

fig = plt.figure(figsize=(10,6))
colors = ['red', 'blue', 'green', 'orange', 'black']
for i in range(5):
    start_index = i*12
    end_index = (i+1)*12
    subset = data[start_index:end_index]
    subset['month'] = [int(str(x).split('-')[1]) for x in subset['date']]
    label = str(1948 + i)
    plt.plot(subset['month'], subset['shiyelv'], c=colors[i], label=label)
plt.legend(loc='upper left')
plt.xlabel('Month, Integer')
plt.ylabel('Unemployment Rate, Percent')
plt.title('Monthly Unemployment Trends, 1948-1952')

plt.show()

 

参考:

https://www.cnblogs.com/qtww56/archive/2018/04.html

https://blog.csdn.net/yimi_ac/article/details/79008555

https://blog.csdn.net/yimi_ac/article/details/79008555

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM