import pandas as pd import numpy as np import matplotlib.pyplot as plt # 用来正常显示中文标签 plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示负号 plt.rcParams['axes.unicode_minus']=False # 读取本地 unrate.csv 文件 unrate = pd.read_csv('unrate.csv') print(unrate.head()) # pd.to_datetime() 将数据转换成datetime类型 unrate['DATE'] = pd.to_datetime(unrate['DATE']) print(unrate.head(12)) # plt.plot()画折线图 plt.plot() # plt.show()显示图形 plt.show()
DATE VALUE 0 1948/1/1 3.4 1 1948/2/1 3.8 2 1948/3/1 4.0 3 1948/4/1 3.9 4 1948/5/1 3.5 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 pandas as pd import numpy as np import matplotlib.pyplot as plt # 用来正常显示中文标签 plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示负号 plt.rcParams['axes.unicode_minus']=False # 读取本地 unrate.csv 文件 unrate = pd.read_csv('unrate.csv') first_twelve = unrate[0:12] print (first_twelve) plt.plot(first_twelve['DATE'], first_twelve['VALUE']) plt.show()
DATE VALUE 0 1948/1/1 3.4 1 1948/2/1 3.8 2 1948/3/1 4.0 3 1948/4/1 3.9 4 1948/5/1 3.5 5 1948/6/1 3.6 6 1948/7/1 3.6 7 1948/8/1 3.9 8 1948/9/1 3.8 9 1948/10/1 3.7 10 1948/11/1 3.8 11 1948/12/1 4.0
import pandas as pd import numpy as np import matplotlib.pyplot as plt # 用来正常显示中文标签 plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示负号 plt.rcParams['axes.unicode_minus']=False # 读取本地 unrate.csv 文件 unrate = pd.read_csv('unrate.csv') first_twelve = unrate[0:12] plt.plot(first_twelve['DATE'], first_twelve['VALUE']) # plt.xticks设置x轴坐标,rotation设置x刻度旋转角度 plt.xticks(rotation=45) #print (help(plt.xticks)) plt.show()
import pandas as pd import numpy as np import matplotlib.pyplot as plt # 用来正常显示中文标签 plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示负号 plt.rcParams['axes.unicode_minus']=False # 读取本地 unrate.csv 文件 unrate = pd.read_csv('unrate.csv') first_twelve = unrate[0:12] plt.plot(first_twelve['DATE'], first_twelve['VALUE']) plt.plot(first_twelve['DATE'], first_twelve['VALUE']) # plt.xticks设置x轴坐标,rotation设置x刻度旋转角度 plt.xticks(rotation=90) # plt.xlabel()设置x轴标题 #plt.xlabel('Month') plt.xlabel('月份') #plt.ylabel('Unemployment rate') plt.ylabel('失业率') # plt.title()设置标题 plt.title('1948年失业率走势') plt.show()
在一张纸上画多张图
import matplotlib.pyplot as plt # 创建画板 fig = plt.figure() #.add_subplot添加子图 ax1 = fig.add_subplot(2,2,1) ax2 = fig.add_subplot(2,2,2) #ax3 = fig.add_subplot(2,2,3) ax4 = fig.add_subplot(2,2,4) #ax4 = fig.add_subplot(224) plt.show()