zoukankan      html  css  js  c++  java
  • matplotlib1

    import pandas as pd
    unrate=pd.read_csv("unrate.csv")
    unrate['DATE']=pd.to_datetime(unrate['DATE'])
    print(unrate.head(12))
    
    >>>
             DATE  VALUE
    0  1948-01-01    3.4
    1  1948-02-01    3.8
    2  1948-03-01    4.0
    3  1948-04-01    3.9
    4  1948-05-01    3.5
    5  1948-06-01    3.6
    6  1948-07-01    3.6
    7  1948-08-01    3.9
    8  1948-09-01    3.8
    9  1948-10-01    3.7
    10 1948-11-01    3.8
    11 1948-12-01    4.0
    读文件
    import matplotlib.pyplot as plt
    plt.plot()#画图
    plt.show()#将画的图显示出来
    
    >>>

    first_year=unrate[0:12]
    plt.plot(first_year['DATE'],first_year['VALUE'])#横轴为'DATE',纵轴为'VALUE'
    plt.show()

    import matplotlib.pyplot as plt
    first_year=unrate[0:12:1]
    plt.plot(first_year['DATE'],first_year['VALUE'])
    plt.xticks(rotation=30) #横坐标倾斜30度
    plt.show()

    plt.plot(first_year['DATE'], first_year['VALUE'])
    plt.xticks(rotation=90)
    plt.xlabel('Month')#横轴标题
    plt.ylabel('Unemployment Rate')#纵轴标题
    plt.title('Monthly Unemployment Trends, 1948')#图形标题
    plt.show()

    import matplotlib.pyplot as plt
    fig=plt.figure()
    ax1=fig.add_subplot(2,2,1)#2行2列第一幅图
    ax2=fig.add_subplot(2,2,2)#2行2列第二幅图
    ax3=fig.add_subplot(2,2,4)#2行2列第四幅图
    plt.show()

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    fig=plt.figure(figsize=(6,6))#图尺寸
    ax1=fig.add_subplot(2,2,1)#2行2列第一幅图
    ax2=fig.add_subplot(2,2,3)#2行2列第三幅图
    ax1.plot(np.random.randint(1,5,5), np.arange(5))#随机生成数字
    ax2.plot(np.arange(5))
    plt.show()

    fig = plt.figure(figsize=(6,3))
    
    plt.plot(unrate[0:12]['DATE'], unrate[0:12]['VALUE'], c='red')#前12个数据为红色
    plt.plot(unrate[12:24]['DATE'], unrate[12:24]['VALUE'], c='blue')#后12个数据为蓝色
    
    plt.show()

    unrate['MONTH'] = unrate['DATE'].dt.month# 也可以将'DATE'转换为数字月份
    
    fig = plt.figure(figsize=(6,3))
    
    plt.plot(unrate[0:12]['MONTH'], unrate[0:12]['VALUE'], c='red')
    plt.plot(unrate[12:24]['MONTH'], unrate[12:24]['VALUE'], c='blue')
    
    plt.show()

    import pandas as pd
    import matplotlib.pyplot as plt
    
    unrate = pd.read_csv('unrate.csv')
    unrate['DATE'] = pd.to_datetime(unrate['DATE'])
    unrate['MONTH'] = unrate['DATE'].dt.month
    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 = unrate[start_index:end_index]
        plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i])
        
    plt.show()

    import pandas as pd
    import matplotlib.pyplot as plt
    
    unrate = pd.read_csv('unrate.csv')
    unrate['DATE'] = pd.to_datetime(unrate['DATE'])
    unrate['MONTH'] = unrate['DATE'].dt.month
    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 = unrate[start_index:end_index]
        plt.plot(subset['DATE'], subset['VALUE'], c=colors[i])  #DATE
        
    plt.show()

    import pandas as pd
    import matplotlib.pyplot as plt
    
    unrate = pd.read_csv('unrate.csv')
    unrate['DATE'] = pd.to_datetime(unrate['DATE'])
    unrate['MONTH'] = unrate['DATE'].dt.month
    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 = unrate[start_index:end_index]
        label=str(1948+i)   #方法二label=1948+i
        plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i],label=label)  #MONTH
    plt.legend(loc="best")   #plt.legend(loc=1)
    
    
    plt.show()

    import pandas as pd
    import matplotlib.pyplot as plt
    
    unrate = pd.read_csv('unrate.csv')
    unrate['DATE'] = pd.to_datetime(unrate['DATE'])
    unrate['MONTH'] = unrate['DATE'].dt.month
    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 = unrate[start_index:end_index]
        label=str(1948+i)
        plt.plot(subset['MONTH'], subset['VALUE'], c=colors[i],label=label)  #MONTH
    plt.legend(loc="best")
    plt.xlabel("Month, Integer")
    plt.ylabel('Unemployment Rate, Percent')
    plt.title('Monthly Unemployment Trends, 1948-1952')
    
    plt.show()

  • 相关阅读:
    MySQL执行外部sql脚本文件的命令
    如何修改mysql 默认引擎为InnoDB?
    最新版的 vscode 怎么配置 Python?
    Go 后端主要做什么
    Go 语言 fmt.Sprintf (格式化输出)
    什么是弱类型语言、强类型语言?
    一个项目从立项到发布的流程
    工厂模式
    观察模式
    类之间的关系
  • 原文地址:https://www.cnblogs.com/muziyi/p/9045111.html
Copyright © 2011-2022 走看看