python升级matplotlib包_Python-matplotlib包
一、matplotlib包
import matplotlib.pyplot as plt
x=[1,2,3,4]
y=[1,4,9,16]
plt.plot(x,y)
plt.show()
'''color:線條顏色,值r表示紅色(red)marker:點的形狀,值o表示點為圓圈標記(circle marker)linestyle:線條的形狀,值dashed表示用虛線連接各點'''
plt.plot(x,y,color='r',marker='o',linestyle='dashed')
'''axis:坐標軸范圍語法為axis[xmin, xmax, ymin, ymax],也就是axis[x軸最小值, x軸最大值, y軸最小值, y軸最大值]'''
plt.axis([0,6,0,20])
plt.show()
如果matplotlib參入的參數只能是列表的話,這對數據處理很不利。一般,我們傳入的是numpy的數組。實際上,所有參入的值內部都會轉換為numpy的數組。
'''arange用于生成一個等差數組,arange([start, ]stop, [step, ]使用見文檔:https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html'''
import numpy as np
t=np.arange(0,5,0.2)
t
array([ 0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2. ,
2.2, 2.4, 2.6, 2.8, 3. , 3.2, 3.4, 3.6, 3.8, 4. , 4.2,
4.4, 4.6, 4.8])
'''使用數組同時繪制多個線性'''
#線條1
x1=y1=t
'''運算符**,表示冪 - 返回x的y次冪,例如10**20表示10的20次方詳細見文檔《Python3運算符》:http://www.runoob.com/python3/python3-basic-operators.html'''
#線條2
x2=x1
y2=t**2
#線條3
x3=x1
y3=t**3
#使用plot繪制線條
lineList=plt.plot(x1,y1,x2,y2,x3,y3)
#用setp方法可以同時設置多個線條的屬性
plt.setp(lineList,color='r')
plt.show()
print('返回的數據類型:',type(lineList))
print('數據大小:',len(lineList))
返回的數據類型:
數據大小: 3
在圖上添加文本
import matplotlib
matplotlib.matplotlib_fname()
'D:\\ana\\lib\\site-packages\\matplotlib\\mpl-data\\matplotlibrc'
x=[1,2,3,4]
y=[1,4,9,16]
plt.plot(x,y)
plt.xlabel('x坐標軸')
plt.ylabel('y坐標軸')
plt.title('標題')
'''添加注釋:參數名xy:箭頭注釋中箭頭所在位置,參數名xytext:注釋文本所在位置,arrowprops在xy和xytext之間繪制箭頭,shrink表示注釋點與注釋文本之間的圖標距離'''
plt.annotate('我是注釋',xy=(2,5),xytext=(2,10),arrowprops=dict(facecolor='black',shrink=0.01))
plt.show()
多個圖繪圖
#創建畫板1
fig=plt.figure(1)
#第2步:創建畫紙(子圖)
'''subplot()方法里面傳入的三個數字前兩個數字代表要生成幾行幾列的子圖矩陣,第三個數字代表選中的子圖位置subplot(211)生成一個2行1列的子圖矩陣,當前是第一個子圖'''
#創建畫紙,并選擇畫紙1
#等價于ax1=fig.add_subplot(211)
ax1=plt.subplot(2,1,1)
#在畫紙上繪圖
plt.plot([1,2,3])
#選擇畫紙2
ax2=plt.subplot(2,1,2)
#在畫紙2上繪圖
plt.plot([4,5,6])
plt.show()
二、數據可視化:股票數據分析
互聯數據獲取包pandas-datareader現在已經使用不了了,所以自己導入數據。
先自己定義一個函數:計算股票漲跌幅
'''定義函數函數功能:計算股票漲跌幅=(現在股價-買入價格)/買入價格輸入參數:column是收盤價這一列的數據返回數據:漲跌幅'''
def change(column):
#買入價格
buyprice=column[0]
#現在股價
#column.size是總共數據條數,序號是從0開始的,所以最后一條數據的序號是總數目-1
curprice=column[column.size-1]
#累計漲跌幅
pricechange=(curprice-buyprice)/buyprice
#判斷股票是上漲,還是下跌
if pricechange>0:
print('股票累計上漲=',pricechange*100,'%')
elif pricechange==0:
print('股票累沒有變化=',pricechange*100,'%')
else:
print('股票累計下跌',pricechange*100,'%')
#返回數據
return pricechange
阿里巴巴
import pandas as pd
#導入數據
filenamestr1='F:/BaiduNetdiskDownload/數據可視化/阿里巴巴2017年股票數據.xlsx'
x1=pd.ExcelFile(filenamestr1)
babadf=x1.parse('Sheet1')
babadf.head()
為了將橫坐標設置成X軸坐標,將Date列設置為索引。
#重置索引,將Date設為索引
babadf.reset_index(inplace=True)
babadf.set_index('Date',inplace=True)
babadf.head()
babadf.info()
DatetimeIndex: 251 entries, 2017-01-03 to 2017-12-29
Data columns (total 7 columns):
index 251 non-null int64
Open 251 non-null float64
High 251 non-null float64
Low 251 non-null float64
Close 251 non-null float64
Adj Close 251 non-null float64
Volume 251 non-null int64
dtypes: float64(5), int64(2)
memory usage: 15.7 KB
babadf.dtypes
index int64
Open float64
High float64
Low float64
Close float64
Adj Close float64
Volume int64
dtype: object
babadf.describe()
'''累計漲幅'''
#獲取收盤價Close這一列的數據
closecol1=babadf['Close']
#調用函數,獲取漲跌幅
babachange=change(closecol1)
股票累計上漲= 94.6162493141 %
谷歌
filenamestr2='F:/BaiduNetdiskDownload/數據可視化/谷歌2017年股票數據.xlsx'
x2=pd.ExcelFile(filenamestr2)
goodf=x2.parse('Sheet1')
goodf.head()
goodf.reset_index(inplace=True)
goodf.set_index('Date',inplace=True)
goodf.head()
'''累計漲幅'''
#獲取收盤價Close這一列的數據
closecol2=goodf['Close']
#調用函數,獲取漲跌幅
goochange=change(closecol2)
股票累計上漲= 33.1060630465 %
亞馬遜
filenamestr3='F:/BaiduNetdiskDownload/數據可視化/亞馬遜2017年股票數據.xlsx'
x3=pd.ExcelFile(filenamestr3)
amazdf=x3.parse('Sheet1')
amazdf.head()
amazdf.reset_index(inplace=True)
amazdf.set_index('Date',inplace=True)
amazdf.head()
'''累計漲幅'''
#獲取收盤價Close這一列的數據
closecol3=amazdf['Close']
#調用函數,獲取漲跌幅
amazchange=change(closecol3)
股票累計上漲= 55.1700342828 %
filenamestr4='F:/BaiduNetdiskDownload/數據可視化/Facebook2017年股票數據.xlsx'
x4=pd.ExcelFile(filenamestr4)
fbdf=x4.parse('Sheet1')
fbdf.head()
fbdf.reset_index(inplace=True)
fbdf.set_index('Date',inplace=True)
fbdf.head()
'''累計漲幅'''
#獲取收盤價Close這一列的數據
closecol4=fbdf['Close']
#調用函數,獲取漲跌幅
fbchange=change(closecol4)
股票累計上漲= 51.0012027126 %
蘋果
filenamestr5='F:/BaiduNetdiskDownload/數據可視化/蘋果2017年股票數據.xlsx'
x5=pd.ExcelFile(filenamestr5)
appdf=x5.parse('Sheet1')
appdf.head()
appdf.reset_index(inplace=True)
appdf.set_index('Date',inplace=True)
appdf.head()
'''累計漲幅'''
#獲取收盤價Close這一列的數據
closecol5=appdf['Close']
#調用函數,獲取漲跌幅
appchange=change(closecol5)
股票累計上漲= 45.6995205217 %
騰訊
filenamestr6='F:/BaiduNetdiskDownload/數據可視化/騰訊2017年股票數據.xlsx'
x6=pd.ExcelFile(filenamestr6)
txdf=x6.parse('Sheet1')
txdf.head()
txdf.reset_index(inplace=True)
txdf.set_index('Date',inplace=True)
txdf.head()
'''累計漲幅'''
#獲取收盤價Close這一列的數據
closecol6=txdf['Close']
#調用函數,獲取漲跌幅
txchange=change(closecol6)
股票累計上漲= 114.361147234 %
數據可視化
%matplotlib inline
import matplotlib.pyplot as plt
折線圖:繪制股票走勢
babadf.plot(y='Close')
plt.xlabel('時間')
plt.ylabel('股價(美元)')
plt.title('2017年阿里巴巴股價走勢')
#顯示網格
plt.grid(True)
plt.show()
散點圖:成交量和股價
'''我們給plot傳入的橫軸x坐標軸數據成交量這一列的數據,縱軸y坐標軸數據是收盤價這一列的數據,同時增加了一個參數叫kind這個值表示繪制圖形的類型,這里的值等于scatter表示繪制散點圖。kind取值(圖形類型)參考官方文檔:http://pandas.pydata.org/pandas-docs/stable/visualization.html'''
babadf.plot(x='Volume',y='Close',kind='scatter')
plt.xlabel('成交量')
plt.ylabel('股價(美元)')
plt.title('成交量和股價')
plt.grid(True)
plt.show()
#得到相關系數矩陣
babadf.corr()
#繪制谷歌的畫紙1
ax1=goodf.plot(y='Close')
#通過指定畫紙ax,在同一張畫紙上繪圖
amazdf.plot(ax=ax1,y='Close')
fbdf.plot(ax=ax1,y='Close')
appdf.plot(ax=ax1,y='Close')
babadf.plot(ax=ax1,y='Close')
txdf.plot(ax=ax1,y='Close')
plt.xlabel('時間')
plt.ylabel('股價(美元)')
plt.title('2017年GAFATA股價累計漲幅比較')
plt.grid(True)
plt.show()
'''使用label自定義圖例'''
#繪制谷歌的畫紙1
ax1=goodf.plot(y='Close',label='谷歌')
#通過指定畫紙ax,在同一張畫紙上繪圖
amazdf.plot(ax=ax1,y='Close',label='亞馬遜')
fbdf.plot(ax=ax1,y='Close',label='Facebook')
appdf.plot(ax=ax1,y='Close',label='蘋果')
babadf.plot(ax=ax1,y='Close',label='阿里巴巴')
txdf.plot(ax=ax1,y='Close',label='騰訊')
plt.xlabel('時間')
plt.ylabel('股價(美元)')
plt.title('2017年GAFATA股價累計漲幅比較')
plt.grid(True)
plt.show()
因為谷歌和亞馬遜的股價比較高,造成我們看不出其他4家公司的股票走勢。 所以根據股價我們可以將這6家公司分成2組,一組是股價較高的谷歌和亞馬遜。另外一組是股價較低的4家公司。
'''第1組:谷歌,亞馬遜'''
#繪制谷歌的畫紙2
ax2=goodf.plot(y='Close',label='谷歌')
#通過指定畫紙ax,在同一張畫紙上繪圖
amazdf.plot(ax=ax2,y='Close',label='亞馬遜')
plt.xlabel('時間')
plt.ylabel('股價(美元)')
plt.title('2017年谷歌和亞馬遜股價累計漲幅比較')
plt.grid(True)
plt.show()
'''第2組:4家公司'''
#繪制Facebook的畫紙3
#通過指定畫紙ax,在同一張畫紙上繪圖
ax3=fbdf.plot(y='Close',label='Facebook')
appdf.plot(ax=ax3,y='Close',label='蘋果')
babadf.plot(ax=ax3,y='Close',label='阿里巴巴')
txdf.plot(ax=ax3,y='Close',label='騰訊')
plt.xlabel('時間')
plt.ylabel('股價(美元)')
plt.title('2017年GAFATA股價累計漲幅比較')
plt.grid(True)
plt.show()
柱狀圖:六家公司股票的平均值
gafatameanlist=[goodf['Close'].mean(),#谷歌
amazdf['Close'].mean(),#亞馬遜
fbdf['Close'].mean(),#Facebook
appdf['Close'].mean(),#蘋果
babadf['Close'].mean(),#阿里巴巴
txdf['Close_dollar'].mean()#騰訊
]
gafatameanser=pd.Series(gafatameanlist,index=['谷歌',
'亞馬遜',
'Facebook',
'蘋果',
'阿里巴巴',
'騰訊'])
gafatameanser.plot(kind='bar',label='GAFATA')
plt.title('2017年GAFATA股價平均值')
plt.xlabel('公司名稱')
plt.ylabel('股價平均值(美元)')
plt.grid(True)
plt.show()
分析結果:可以看出,僅從股票價格上來判斷,亞馬遜和谷歌的股票價格要遠遠的超過了其他四家。但是這里只是算的平均值,下面我們看下用四分位數繪制的箱線圖
#存放6家公司的收盤價
closedf=pd.DataFrame()
#合并6家公司的收盤價
closedf=pd.concat([closedf,goodf['Close'],#谷歌
amazdf['Close'],#亞馬遜
fbdf['Close'],#Facebook
appdf['Close'],#蘋果
babadf['Close'],#阿里巴巴
txdf['Close_dollar']#騰訊
],axis=1)
closedf.columns=['谷歌','亞馬遜','Facebook','Facebook','阿里巴巴','阿里巴巴']
closedf.head()
#箱線圖
closedf.plot(kind='box')
plt.grid(True)
plt.show()
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python升级matplotlib包_Python-matplotlib包的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 获取浏览器高度_QQ浏览器违法收集用户信
- 下一篇: python list遍历删除_Pyth