(本人用的jupyter,所以以下格式是以jupyter格式书写的)
可视化库matpltlib:
一、折线图绘制
import pandas as pd
unrate = pd.read_csv('unrate.csv') # 读取
print (type(unrate['DATE'][0]))
unrate['DATE'] = pd.to_datetime(unrate['DATE']) #将字符串转化成时间类型
print (unrate.head(12))
输出:
import matplotlib.pyplot as plt
#%matplotlib inline
#Using the different pyplot functions, we can create, customize, and display a plot. For example, we can use 2 functions to :
plt.plot() # 画图操作,如果没有传参数,则什么都不画
plt.show()# 图像显示
first_twelve = unrate[0:12]
plt.plot(first_twelve['DATE'], first_twelve['VALUE']) #(x,y)
plt.show()
这里可以看出,我们横坐标不好看,下面将对其进行处理:
plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.xticks(rotation=45) # 修改坐标书写方式,这里表示选择45度
#print help(plt.xticks)
plt.show()
但我们的图表的横纵坐标还不能清除的表达出表的意思,下面变为其添加标题:
plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.xticks(rotation=90)
plt.xlabel('Month') # 横坐标标签
plt.ylabel('Unemployment Rate')
plt.title('Monthly Unemployment Trends, 1948') #表的标题
plt.show()
二、子图操作
相关函数:
add_subplot(4,1,x) #添加子图,4,1表示4行1列
添加子图:
#add_subplot(first,second,index) first means number of Row,second means number of Column.
import matplotlib.pyplot as plt
fig = plt.figure() # 默认画图区间,创建画图域
ax1 = fig.add_subplot(3,2,1) # 添加子图,3,2表示3*2的区间,1表示这个子图在区间的位置
ax2 = fig.add_subplot(3,2,2)
ax2 = fig.add_subplot(3,2,6)
plt.show()
import numpy as np
fig = plt.figure()
fig = plt.figure(figsize=(3, 3)) # 指定画图域的长和宽的大小
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))
# np.random.randint(1,5,5) 这是numpy的一个方法,第三个参数为产生随机数的个数
ax2.plot(np.arange(10)*3, np.arange(10))
plt.show()
为一个表添加多个对象,以便于比较: