python matplotlib畫(huà)圖實(shí)例代碼分享
python的matplotlib包支持我們畫(huà)圖,有點(diǎn)非常多,現(xiàn)學(xué)習(xí)如下。
首先要導(dǎo)入包,在以后的示例中默認(rèn)已經(jīng)導(dǎo)入這兩個(gè)包
import matplotlib.pyplot as plt import numpy as np
然后畫(huà)一個(gè)最基本的圖
t = np.arange(0.0, 2.0, 0.01)#x軸上的點(diǎn),0到2之間以0.01為間隔 s = np.sin(2*np.pi*t)#y軸為正弦 plt.plot(t, s)#畫(huà)圖 plt.xlabel('time (s)')#x軸標(biāo)簽 plt.ylabel('voltage (mV)')#y軸標(biāo)簽 plt.title('About as simple as it gets, folks')#圖的標(biāo)簽 plt.grid(True)#產(chǎn)生網(wǎng)格 plt.savefig("test.png")#保存圖像 plt.show()#顯示圖像
這是在一個(gè)窗口中畫(huà)單張圖的過(guò)程,那么如何畫(huà)多張圖呢?畫(huà)圖的過(guò)程相同,無(wú)非是畫(huà)多張,然后設(shè)定位置。
x1 = np.linspace(0.0, 5.0)#畫(huà)圖一 x2 = np.linspace(0.0, 2.0)#畫(huà)圖二 y1 = np.cos(2 * np.pi * x1) * np.exp(-x1) y2 = np.cos(2 * np.pi * x2) plt.subplot(2, 1, 1)#面板設(shè)置成2行1列,并取第一個(gè)(順時(shí)針編號(hào)) plt.plot(x1, y1, 'yo-')#畫(huà)圖,染色 plt.title('A tale of 2 subplots') plt.ylabel('Damped oscillation') plt.subplot(2, 1, 2)#面板設(shè)置成2行1列,并取第二個(gè)(順時(shí)針編號(hào)) plt.plot(x2, y2, 'r.-')#畫(huà)圖,染色 plt.xlabel('time (s)') plt.ylabel('Undamped') plt.show()
兩張圖的示例如下
直方圖的畫(huà)法
# -*- coding:utf-8 -*- import numpy as np import matplotlib.mlab as mlab import matplotlib.pyplot as plt mu = 100 # 正態(tài)分布的均值 sigma = 15 # 標(biāo)準(zhǔn)差 x = mu + sigma * np.random.randn(10000)#在均值周圍產(chǎn)生符合正態(tài)分布的x值 num_bins = 50 n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5) #直方圖函數(shù),x為x軸的值,normed=1表示為概率密度,即和為一,綠色方塊,色深參數(shù)0.5.返回n個(gè)概率,直方塊左邊線的x值,及各個(gè)方塊對(duì)象 y = mlab.normpdf(bins, mu, sigma)#畫(huà)一條逼近的曲線 plt.plot(bins, y, 'r--') plt.xlabel('Smarts') plt.ylabel('Probability') plt.title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')#中文標(biāo)題 u'xxx' plt.subplots_adjust(left=0.15)#左邊距 plt.show()
直方圖如下
3D圖像的畫(huà)法
3D離散點(diǎn)
#!/usr/bin/env python # encoding: utf-8 import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D x_list = [[3,3,2],[4,3,1],[1,2,3],[1,1,2],[2,1,2]] fig = plt.figure() ax = fig.gca(projection='3d') for x in x_list: ax.scatter(x[0],x[1],x[2],c='r') plt.show()
畫(huà)空間平面
from mpl_toolkits.mplot3d.axes3d import Axes3D from matplotlib import cm import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(1, 1, 1, projection='3d') X=np.arange(1,10,1) Y=np.arange(1,10,1) X, Y = np.meshgrid(X, Y) Z = 3*X+2*Y+30 surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1,cmap=cm.jet,linewidth=0, antialiased=True) ax.set_zlim3d(0,100) fig.colorbar(surf, shrink=0.5, aspect=5) plt.show()
畫(huà)空間曲面
from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.gca(projection='3d') X = np.arange(-5, 5, 0.1) Y = np.arange(-5, 5, 0.1) X, Y = np.meshgrid(X, Y) R = np.sqrt(X**2 + Y**2) Z = np.sin(R) surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False) #畫(huà)表面,x,y,z坐標(biāo), 橫向步長(zhǎng),縱向步長(zhǎng),顏色,線寬,是否漸變 ax.set_zlim(-1.01, 1.01)#坐標(biāo)系的下邊界和上邊界 ax.zaxis.set_major_locator(LinearLocator(10))#設(shè)置Z軸標(biāo)度 ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))#Z軸精度 fig.colorbar(surf, shrink=0.5, aspect=5)#shrink顏色條伸縮比例(0-1),aspect顏色條寬度(反比例,數(shù)值越大寬度越窄) plt.show()
3D圖分別如下
餅狀圖畫(huà)法
# -*- coding: utf-8 -*- import matplotlib.pyplot as plt labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'#設(shè)置標(biāo)簽 sizes = [15, 30, 45, 10]#占比,和為100 colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']#顏色 explode = (0, 0.1, 0, 0) #展開(kāi)第二個(gè)扇形,即Hogs,間距為0.1 plt.pie(sizes, explode=explode, labels=labels, colors=colors,autopct='%1.1f%%', shadow=True, startangle=90)#startangle控制餅狀圖的旋轉(zhuǎn)方向 plt.axis('equal')#保證餅狀圖是正圓,否則會(huì)有一點(diǎn)角度偏斜 fig = plt.figure() ax = fig.gca() import numpy as np ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors,autopct='%1.1f%%', shadow=True, startangle=90, radius=0.25, center=(0, 0), frame=True) ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90, radius=0.25, center=(1, 1), frame=True) ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90, radius=0.25, center=(0, 1), frame=True) ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90,radius=0.25, center=(1, 0), frame=True) ax.set_xticks([0, 1])#設(shè)置位置 ax.set_yticks([0, 1]) ax.set_xticklabels(["Sunny", "Cloudy"])#設(shè)置標(biāo)簽 ax.set_yticklabels(["Dry", "Rainy"]) ax.set_xlim((-0.5, 1.5)) ax.set_ylim((-0.5, 1.5)) ax.set_aspect('equal') plt.show()
餅狀圖如下:
平時(shí)用到的也就這幾種,掌握這幾種就差不多了,更多內(nèi)容見(jiàn)
https://matplotlib.org/users/screenshots.html
總結(jié)
以上就是本文關(guān)于python matplotlib畫(huà)圖實(shí)例代碼分享的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
- python Matplotlib畫(huà)圖之調(diào)整字體大小的示例
- Python matplotlib畫(huà)圖時(shí)圖例說(shuō)明(legend)放到圖像外側(cè)詳解
- Python matplotlib實(shí)時(shí)畫(huà)圖案例
- Python畫(huà)柱狀統(tǒng)計(jì)圖操作示例【基于matplotlib庫(kù)】
- python matplotlib畫(huà)圖庫(kù)學(xué)習(xí)繪制常用的圖
- python使用Matplotlib畫(huà)條形圖
- python使用Matplotlib畫(huà)餅圖
- python?matplotlib各種畫(huà)圖
- Python中使用Matplotlib進(jìn)行多圖繪制的詳細(xì)教程
相關(guān)文章
Python實(shí)現(xiàn)求兩個(gè)數(shù)組交集的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)求兩個(gè)數(shù)組交集的方法,涉及Python數(shù)組遍歷、排序、判斷、追加等相關(guān)操作技巧,需要的朋友可以參考下2019-02-02Win8.1下安裝Python3.6提示0x80240017錯(cuò)誤的解決方法
這篇文章主要為大家詳細(xì)介紹了Win8.1下安裝Python3.6提示0x80240017錯(cuò)誤的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07python數(shù)據(jù)預(yù)處理方式 :數(shù)據(jù)降維
今天小編就為大家分享一篇python數(shù)據(jù)預(yù)處理方式 :數(shù)據(jù)降維,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02