山南網(wǎng)站建設(shè)網(wǎng)絡(luò)平臺(tái)推廣方案
pandas——plot()方法可視化
作者:AOAIYI
創(chuàng)作不易,如果覺得文章不錯(cuò)或能幫助到你學(xué)習(xí),記得點(diǎn)贊收藏評(píng)論哦
在此,感謝你的閱讀
文章目錄
- pandas——plot()方法可視化
- 一、實(shí)驗(yàn)?zāi)康?/li>
- 二、實(shí)驗(yàn)原理
- 三、實(shí)驗(yàn)環(huán)境
- 四、實(shí)驗(yàn)內(nèi)容
- 五、實(shí)驗(yàn)步驟
一、實(shí)驗(yàn)?zāi)康?/h1>
熟練掌握使用pandas中數(shù)據(jù)用plot方法繪制圖
二、實(shí)驗(yàn)原理
繪圖方法允許除了默認(rèn)的線圖之外的一些繪圖樣式,這些方法可以通過plot()的關(guān)鍵字參數(shù)kind提供。這些包括:
bar 、barh:繪制條形圖
hist:繪制直方圖
box:繪制箱型圖
kde、density:繪制密度圖
area:面積圖
scatter:繪制散點(diǎn)圖
hexbin:棱形圖
pie:繪制餅圖
三、實(shí)驗(yàn)環(huán)境
Python 3.6.0以上
Jupyter
四、實(shí)驗(yàn)內(nèi)容
練習(xí)使用pandas中數(shù)據(jù)用plot方法繪制圖。
五、實(shí)驗(yàn)步驟
1.編寫代碼,使用Series的plot繪制Series中數(shù)據(jù)的分布圖
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
ts = pd.Series(np.random.randn(1000),index=pd.date_range('1/1/2000',periods=1000)) #創(chuàng)建一個(gè)Series
ts = ts.cumsum() #對(duì)Series數(shù)據(jù)進(jìn)行累加求和
ts.plot() #使用plot方法繪制Series中數(shù)據(jù)分布圖
plt.show()
2.創(chuàng)建一個(gè)DataFrame名為df,使用df的plot繪制df中數(shù)據(jù)的分布圖,代碼如下:
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltts = pd.Series(np.random.randn(1000),index=pd.date_range('1/1/2000',periods=1000)) #創(chuàng)建一個(gè)Series
df = pd.DataFrame(np.random.randn(1000,4),index=ts.index,columns=list('ABCD')) #創(chuàng)建一個(gè)DataFrame
df = df.cumsum() #對(duì)df數(shù)據(jù)進(jìn)行累加求和df.plot()
plt.show()
3.創(chuàng)建一個(gè)DataFrame名為df,使用df的plot方法繪制df第6行數(shù)據(jù)的條形圖,代碼如下:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
ts = pd.Series(np.random.randn(1000),index=pd.date_range('1/1/2000',periods=1000)) #創(chuàng)建一個(gè)Series
df = pd.DataFrame(np.random.randn(1000,4),index=ts.index,columns=list('ABCD')) #創(chuàng)建一個(gè)DataFrame
df = df.cumsum() #對(duì)df數(shù)據(jù)進(jìn)行累加求和
df.iloc[5].plot(kind='bar')
plt.axhline(0,color='k')
plt.show()
4.創(chuàng)建一個(gè)DataFrame名為df2,使用df2的plot.bar()方法繪制df2數(shù)據(jù)的條形圖,代碼如下:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df2 = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d'])
df2.plot.bar()
plt.show()
5.使用plot.bar方法對(duì)上述df2數(shù)據(jù)繪制一個(gè)堆疊的條形圖,通過設(shè)置參數(shù)stacked=True,代碼如下:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df2 = pd.DataFrame(np.random.rand(10,4),columns=['a','b','c','d'])
df2.plot.bar(stacked=True)
plt.show()
6.使用plot.barh方法對(duì)上述df2數(shù)據(jù),通過設(shè)置參數(shù)stacked=True,繪制一個(gè)水平堆疊條形圖,代碼如下:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df2 = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df2.plot.barh(stacked=True)
plt.show()
7.創(chuàng)建一個(gè)DataFrame名為df3,使用df3的plot.hist()方法繪制df3數(shù)據(jù)的直方圖,代碼如下:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df3 = pd.DataFrame({'a': np.random.randn(1000) + 1, 'b': np.random.randn(1000),'c': np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
df3.plot.hist(alpha=0.5)
plt.show()
8.使用plot.hist()方法對(duì)上述df3數(shù)據(jù),通過設(shè)置堆疊參數(shù)stacked=True,設(shè)置條數(shù)大小參數(shù)bins=20,繪制一個(gè)堆疊直方圖,代碼如下:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df3 = pd.DataFrame({'a': np.random.randn(1000) + 1, 'b': np.random.randn(1000),'c': np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])
df3.plot.hist(stacked=True, bins=20)
plt.show()
9.創(chuàng)建一個(gè)DataFrame名為df4,使用df4的plot.box()方法繪制df3數(shù)據(jù)的箱型圖,代碼如下:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df4 = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
df4.plot.box()
plt.show()
10.創(chuàng)建一個(gè)DataFrame名為df5,使用df5的plot.scatter()方法繪制df5數(shù)據(jù)的散點(diǎn)圖,代碼如下:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df5 = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
df5.plot.scatter(x='a', y='b');
plt.show()
11.創(chuàng)建一個(gè)DataFrame名為df6,使用df6的plot.pie()方法繪制df6數(shù)據(jù)的餅圖,代碼如下:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df6 = pd.DataFrame(3 * np.random.rand(4, 2), index=['a', 'b', 'c', 'd'], columns=['x', 'y'])
df6.plot.pie(subplots=True, figsize=(8, 4))
plt.show()