美文网首页
可视化折线图matplotlib 以及python3常用內置函數

可视化折线图matplotlib 以及python3常用內置函數

作者: wong11 | 来源:发表于2017-10-05 16:53 被阅读0次

1.折线图

数据.png
##读入数据
import pandas as pd
import matplotlib.pyplot as plt
unrate = pd.read_csv('unrate.csv')
#用pandas中的方法处理DATE数据
unrate['DATE'] = pd.to_datetime(unrate['DATE'])
##查看前十二行
print(unrate.head(12))
##绘制前12行的折线图
first_twelve = unrate[0:12]
##画图
plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
##显示图
plt.show()
折线图1.png
##x轴下标看不清,可以旋转x下标45度
import matplotlib.pyplot as plt
plt.plot(first_twelve['DATE'], first_twelve['VALUE'])
plt.xticks(rotation=45)
plt.show()
折线图2.png
##标题,x,y轴标签
import matplotlib.pyplot as plt
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()
折线图3.png
#子图
import matplotlib.pyplot as plt
#先构建一个默认的区间
fig = plt.figure()
#在区间中添加子图
ax1 = fig.add_subplot(3,2,1)
ax2 = fig.add_subplot(3,2,2)
ax2 = fig.add_subplot(3,2,4)
plt.show()
子图1.png
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
#给画图区域一个指定大小为3 * 3,长度 * 宽度
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))
ax2.plot(np.arange(10)*3, np.arange(10))
plt.show()
子图2.png
import matplotlib.pyplot as plt
unrate['MONTH'] = unrate['DATE'].dt.month
unrate['MONTH'] = unrate['DATE'].dt.month
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()
颜色.png
import matplotlib.pyplot as plt
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()
多折线图.png
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)
plt.legend(loc='upper left')
plt.xlabel('Month, Integer')
plt.ylabel('Unemployment Rate, Percent')
plt.title('Monthly Unemployment Trends, 1948-1952')

plt.show()
标签

python3 函數測速

import time  
from functools import reduce

#循環map和列表生成式速度比較
t1 = time.time()
#循环
a = []
array = range(100000)
for i in array:
    a.append(2*(i+1))
print (a)
t2 = time.time()

t3 = time.time()
#map函数
array = range(100000)
a = map(lambda x: 2*(x+1), array)
#python2中map直接返回一个列表
#python3中map改成了惰性函数,想要返回列表需要用list转换
print (list(a))
t4 = time.time()

t5= time.time()
#列表推导
array = range(100000)
a = [2*(x+1) for x in array]
print (a)
t6= time.time()



#循環和reduce速度比較
t7= time.time()
a = 0
array = range(100000)
for i in range(len(array)):
    a = a + array[i]
t8= time.time() 

t9= time.time()
array = range(100000)
a = reduce(lambda x,y: x+y , array)
t10= time.time() 



timefor = t2 - t1
timemap = t4 - t3
timelist = t6 - t5
timeforsum = t8 - t7
timereduce = t10 - t9


print ('for:',timefor,'map',timemap,'list',timelist,'forsum',timeforsum,'reduce',timereduce)

#結果:for: 0.09300518035888672 
#map 0.08100461959838867 
#list 0.0630037784576416
#結果:forsum 0.18001055717468262 
#reduce 0.0390019416809082

可以看出 循環和map效率相似,map略快,列表生成式速度最快。累加計算 循環明顯慢與reduce。

相关文章

网友评论

      本文标题:可视化折线图matplotlib 以及python3常用內置函數

      本文链接:https://www.haomeiwen.com/subject/agscyxtx.html