MatplotlibTutorial——Matplotlib的基本使用【Jupiter Notebook代码】
生活随笔
收集整理的這篇文章主要介紹了
MatplotlibTutorial——Matplotlib的基本使用【Jupiter Notebook代码】
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
加載必要的包
import numpy as np import pandas as pd import matplotlib.pyplot as plt基本線圖繪圖(學會查文檔)
# 數(shù)據(jù)輸入 x = [0,1,2,3,4] y = [0,1,2,3,4]# 設(shè)置圖片大小 plt.figure(figsize=(5,3),dpi=200)# 畫第一個函數(shù) plt.plot(x,y,label='y=x',color='green',marker='^',markersize=10,linewidth=1,linestyle='-') # 加label后面才能顯示legend# 畫第二個分段函數(shù)(有一個預測段) x2 = np.arange(0,4.5,0.5) plt.plot(x2[:3],x2[:3]**2,label='y=x^2',marker='*',color='r') plt.plot(x2[2:],x2[2:]**2,label='y=x^2,prediction',marker='*',color='r',linestyle='--')# 設(shè)置標題和坐標軸 plt.title("Our First Graph!",fontdict={'fontsize': 20}) plt.xlabel("X Axis",fontdict={'fontsize':9}) plt.ylabel("Y Axis",fontdict={'fontsize':9})# 設(shè)置坐標軸的刻度(或者刻度標簽) #plt.xticks([0,1,1.5,2,3,3.5,4]) #plt.yticks([0,1,2,3,4])# 輸出前設(shè)置圖例和網(wǎng)格 plt.legend() plt.grid()# 保存圖片(先保存,后展示) plt.savefig('mygraph.jpg',dpi=700)plt.show()基本條形圖繪圖
# 輸入數(shù)據(jù) labels = ['A','B','C'] values = [1,4,2]# 基本繪圖 bars = plt.bar(labels,values)# 高階的奇葩設(shè)置 #bars[0].set_hatch('/') #bars[1].set_hatch('o') #bars[2].set_hatch('*') patterns = ['/','o','*'] for bar in bars:bar.set_hatch(patterns.pop(0)) # 這種方式更高級了一點# 亂七八糟的設(shè)置 plt.title("My Second Graph!",fontdict={'fontsize': 20}) plt.legend()plt.show()實例教學一:汽油價格
gas = pd.read_csv('./matplotlib_tutorial_download/gas_prices.csv') gas.head()| 1990 | NaN | 1.87 | 3.63 | 2.65 | 4.59 | 3.16 | 1.00 | 2.05 | 2.82 | 1.16 |
| 1991 | 1.96 | 1.92 | 3.45 | 2.90 | 4.50 | 3.46 | 1.30 | 2.49 | 3.01 | 1.14 |
| 1992 | 1.89 | 1.73 | 3.56 | 3.27 | 4.53 | 3.58 | 1.50 | 2.65 | 3.06 | 1.13 |
| 1993 | 1.73 | 1.57 | 3.41 | 3.07 | 3.68 | 4.16 | 1.56 | 2.88 | 2.84 | 1.11 |
| 1994 | 1.84 | 1.45 | 3.59 | 3.52 | 3.70 | 4.36 | 1.48 | 2.87 | 2.99 | 1.11 |
實例教學二:FIFA球隊
import numpy as np import pandas as pd import matplotlib.pyplot as plt fifa = pd.read_csv('./matplotlib_tutorial_download/fifa_data.csv') fifa.head()| 0 | 158023 | L. Messi | 31 | https://cdn.sofifa.org/players/4/19/158023.png | Argentina | https://cdn.sofifa.org/flags/52.png | 94 | 94 | FC Barcelona | ... | 96.0 | 33.0 | 28.0 | 26.0 | 6.0 | 11.0 | 15.0 | 14.0 | 8.0 | €226.5M |
| 1 | 20801 | Cristiano Ronaldo | 33 | https://cdn.sofifa.org/players/4/19/20801.png | Portugal | https://cdn.sofifa.org/flags/38.png | 94 | 94 | Juventus | ... | 95.0 | 28.0 | 31.0 | 23.0 | 7.0 | 11.0 | 15.0 | 14.0 | 11.0 | €127.1M |
| 2 | 190871 | Neymar Jr | 26 | https://cdn.sofifa.org/players/4/19/190871.png | Brazil | https://cdn.sofifa.org/flags/54.png | 92 | 93 | Paris Saint-Germain | ... | 94.0 | 27.0 | 24.0 | 33.0 | 9.0 | 9.0 | 15.0 | 15.0 | 11.0 | €228.1M |
| 3 | 193080 | De Gea | 27 | https://cdn.sofifa.org/players/4/19/193080.png | Spain | https://cdn.sofifa.org/flags/45.png | 91 | 93 | Manchester United | ... | 68.0 | 15.0 | 21.0 | 13.0 | 90.0 | 85.0 | 87.0 | 88.0 | 94.0 | €138.6M |
| 4 | 192985 | K. De Bruyne | 27 | https://cdn.sofifa.org/players/4/19/192985.png | Belgium | https://cdn.sofifa.org/flags/7.png | 91 | 92 | Manchester City | ... | 88.0 | 68.0 | 58.0 | 51.0 | 15.0 | 13.0 | 5.0 | 10.0 | 13.0 | €196.4M |
5 rows × 89 columns
畫直方圖
bins = np.arange(40,110,10)#plt.hist(fifa.Overall,bins=bins,color='#A87632') # 這里的顏色可以在瀏覽器搜索顏色選擇器進行選擇,然后copy HEX code就好了 plt.hist(fifa.Overall,bins=bins,color='#000000') # 黑色plt.xticks(bins) plt.yticks(np.arange(0,11000,1000))plt.xlabel("Skill levels") plt.ylabel("Number of Players")plt.title("Distribution of Player Skills in FIFA 2018")plt.show()餅圖 I
fifa['Preferred Foot'] 0 Left 1 Right 2 Right 3 Right 4 Right 5 Right 6 Right 7 Right 8 Right 9 Right 10 Right 11 Right 12 Right 13 Left 14 Right 15 Left 16 Right 17 Left 18 Right 19 Left 20 Right 21 Right 22 Right 23 Right 24 Left 25 Right 26 Left 27 Right 28 Left 29 Right... 18177 Right 18178 Right 18179 Right 18180 Right 18181 Right 18182 Right 18183 Right 18184 Right 18185 Right 18186 Right 18187 Right 18188 Right 18189 Right 18190 Right 18191 Left 18192 Right 18193 Right 18194 Right 18195 Right 18196 Right 18197 Right 18198 Right 18199 Right 18200 Left 18201 Left 18202 Right 18203 Right 18204 Right 18205 Right 18206 Right Name: Preferred Foot, Length: 18207, dtype: object left = fifa.loc[fifa['Preferred Foot']=='Left'].count()[0] print(left) right = fifa.loc[fifa['Preferred Foot']=='Right'].count()[0] print(right) 4211 13948 labels = ['Left','Right'] colors = ['#abcdef','#aabbcc'] plt.pie([left,right],labels=labels,colors=colors,autopct='%.2f %%') # 注意這里面是個方括號plt.title("Foot Preference of FIFA players")plt.show()餅圖II
fifa.Weight.head() 0 159.0 1 183.0 2 150.0 3 168.0 4 154.0 Name: Weight, dtype: float64 # 去掉這個磅的符號,并且變成一個浮點數(shù) fifa.Weight = [int(x.strip('lbs')) if type(x)==str else x for x in fifa.Weight] fifa.Weight[0] 159.0 light = fifa.loc[fifa.Weight<125].count()[0] print(light) light_medium = fifa.loc[(fifa.Weight>=125) & (fifa.Weight<150)].count()[0] print(light_medium) medium = fifa.loc[(fifa.Weight>=150) & (fifa.Weight<175)].count()[0] print(medium) medium_heavy = fifa.loc[(fifa.Weight>=175) & (fifa.Weight<200)].count()[0] print(medium_heavy) heavy = fifa.loc[fifa.Weight>=200].count()[0] print(heavy)plt.figure(figsize=(5,5),dpi=100)#plt.style.use('default') plt.style.use('ggplot') # 直接用其他的一些風格weights = [light,light_medium,medium,heavy,medium_heavy] labels = ['light','light_medium','medium','heavy','medium_heavy']explode = [.4,.1,.1,.4,.1] # 讓兩個少的往外一點點plt.title("Weight Distrubution of FIFA Players (in lbs)")plt.pie(weights,labels=labels,autopct="%.2f %%",explode=explode)plt.show() 41 2290 10876 4583 369畫箱形圖
fifa.head()| 0 | 158023 | L. Messi | 31 | https://cdn.sofifa.org/players/4/19/158023.png | Argentina | https://cdn.sofifa.org/flags/52.png | 94 | 94 | FC Barcelona | ... | 96.0 | 33.0 | 28.0 | 26.0 | 6.0 | 11.0 | 15.0 | 14.0 | 8.0 | €226.5M |
| 1 | 20801 | Cristiano Ronaldo | 33 | https://cdn.sofifa.org/players/4/19/20801.png | Portugal | https://cdn.sofifa.org/flags/38.png | 94 | 94 | Juventus | ... | 95.0 | 28.0 | 31.0 | 23.0 | 7.0 | 11.0 | 15.0 | 14.0 | 11.0 | €127.1M |
| 2 | 190871 | Neymar Jr | 26 | https://cdn.sofifa.org/players/4/19/190871.png | Brazil | https://cdn.sofifa.org/flags/54.png | 92 | 93 | Paris Saint-Germain | ... | 94.0 | 27.0 | 24.0 | 33.0 | 9.0 | 9.0 | 15.0 | 15.0 | 11.0 | €228.1M |
| 3 | 193080 | De Gea | 27 | https://cdn.sofifa.org/players/4/19/193080.png | Spain | https://cdn.sofifa.org/flags/45.png | 91 | 93 | Manchester United | ... | 68.0 | 15.0 | 21.0 | 13.0 | 90.0 | 85.0 | 87.0 | 88.0 | 94.0 | €138.6M |
| 4 | 192985 | K. De Bruyne | 27 | https://cdn.sofifa.org/players/4/19/192985.png | Belgium | https://cdn.sofifa.org/flags/7.png | 91 | 92 | Manchester City | ... | 88.0 | 68.0 | 58.0 | 51.0 | 15.0 | 13.0 | 5.0 | 10.0 | 13.0 | €196.4M |
5 rows × 89 columns
barcelona = fifa.loc[fifa.Club=='FC Barcelona']['Overall'] madrid = fifa.loc[fifa.Club=='Real Madrid']['Overall'] revs = fifa.loc[fifa.Club=='New England Revolution']['Overall']labels = ['FC Barcelona','Real Madrid','New England Revolution']plt.style.use('default')plt.figure(figsize=(7,10))boxes = plt.boxplot([barcelona, madrid, revs],labels=labels, patch_artist=True) #設(shè)置這個True的原因是后面要設(shè)置facecolor for box in boxes['boxes']: # 方括號內(nèi)的是參數(shù)# 設(shè)置box邊界的顏色box.set(color='#4286f4',linewidth=2)# 設(shè)置box內(nèi)部顏色box.set(facecolor='#e0e0e0')plt.title("Professional Soccer Team Comparison") plt.ylabel("FIFA Overall Rating")plt.show() /home/chen/anaconda3/lib/python3.6/site-packages/numpy/core/fromnumeric.py:57: FutureWarning: reshape is deprecated and will raise in a subsequent release. Please use .values.reshape(...) insteadreturn getattr(obj, method)(*args, **kwds)總結(jié)
以上是生活随笔為你收集整理的MatplotlibTutorial——Matplotlib的基本使用【Jupiter Notebook代码】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言——三目运算符的进阶用法,比较三个
- 下一篇: 剑指chatGPT,马斯克:你们暂停一下