19、20_散点图、连接散点图、气泡图、不同颜色的散点图、直方图
19.散點(diǎn)圖(Scatter plots)
19.1.連接散點(diǎn)圖 (Connected scatter plot)
19.2.氣泡圖(Bubble chart)
19.3.不同顏色的散點(diǎn)圖
20.直方圖(Histograms)
19.散點(diǎn)圖(Scatter plots)
當(dāng)想顯示兩個(gè)變量之間的關(guān)系時(shí),使用散點(diǎn)圖。 散點(diǎn)圖有時(shí)稱(chēng)為相關(guān)圖,因?yàn)樗鼈冿@示了兩個(gè)變量之間的關(guān)系。
import numpy as np import matplotlib.pyplot as pltplt.scatter(x=range(77, 770, 10),y=np.random.randn(70)*55+range(77, 770, 10),s=200, alpha=0.6) plt.tick_params(labelsize=12) plt.xlabel('Surface(m2)', size=12) plt.ylabel('Turnover (K dollars)', size=12) plt.xlim(left=0) plt.ylim(bottom=0)plt.show()
此圖描述了商店面積與其營(yíng)業(yè)額之間的正相關(guān)關(guān)系。
此圖表明客戶的年齡與其每周購(gòu)買(mǎi)費(fèi)用之間沒(méi)有關(guān)心。
19.1.連接散點(diǎn)圖 (Connected scatter plot)
連接的散點(diǎn)圖是散點(diǎn)圖和折線圖之間的混合,它使用線段來(lái)連接連續(xù)的散點(diǎn)圖點(diǎn),例如以說(shuō)明隨時(shí)間變化的軌跡。
連接的散點(diǎn)圖可視化了散點(diǎn)圖中的兩個(gè)相關(guān)時(shí)間序列,并將這些點(diǎn)與時(shí)間順序上的線連接起來(lái)。
import numpy as np import matplotlib.pyplot as pltturnover = [30, 38, 26, 20, 21, 15, 8, 5, 3, 9, 25, 27] plt.plot(np.arange(12), turnover, marker='o')plt.show()假設(shè)上面的圖描述了一年內(nèi)銷(xiāo)售營(yíng)業(yè)額。 根據(jù)該圖,我們可以發(fā)現(xiàn)銷(xiāo)售在冬天達(dá)到頂峰,然后從春季到夏季下降。
19.2.氣泡圖(Bubble chart)
氣泡圖是一種顯示三個(gè)維度的數(shù)據(jù)的圖。一個(gè)附加變量的值通過(guò)點(diǎn)的大小表示。
import numpy as np import matplotlib.pyplot as pltnbclients = range(10, 494, 7) plt.scatter(x=range(77, 770, 10),y=np.random.randn(70)*55+range(77, 770, 10),s=nbclients, alpha=0.6)plt.show()19.3.不同顏色的散點(diǎn)圖
由matplotlib創(chuàng)建的散點(diǎn)圖無(wú)法根據(jù)類(lèi)別變量的值指定顏色。 因此,我們必須重疊不同顏色的圖。
import numpy as np import matplotlib.pyplot as pltplt.scatter(x=range(40, 70, 1),y=np.abs(np.random.randn(30)*20),s=200,#c = 'blue',alpha=0.6,label='40-69') plt.scatter(x=range(20, 40, 1),y=np.abs(np.random.randn(20)*40),s=200,#c = 'red',alpha=0.6,label='20-39') plt.legend() # 每次都要執(zhí)行 plt.show()這個(gè)2色散點(diǎn)圖清楚地顯示了年輕人與中年人或老年人之間的每周購(gòu)買(mǎi)花費(fèi)的差異:年輕人的平均每周購(gòu)買(mǎi)量是中年人或老年人的兩倍。
20.直方圖(Histograms)
直方圖是一種統(tǒng)計(jì)報(bào)告圖,是一些數(shù)值數(shù)據(jù)的頻率分布的圖形表示。由一系列高度不等的縱向條紋或線段表示數(shù)據(jù)分布的情況。 一般用橫軸表示數(shù)據(jù)類(lèi)型,縱軸表示分布情況。
直方圖是數(shù)值數(shù)據(jù)分布的圖形表示,是一個(gè)連續(xù)變量的概率分布的估計(jì)。
為了構(gòu)建直方圖,第一步是將值的范圍分段,即將整個(gè)值的范圍分成一系列間隔,然后計(jì)算每個(gè)間隔中有多少值。 這些值通常被指定為連續(xù)的,不重疊的變量間隔。 間隔必須相鄰,并且通常是相等的大小。
如果構(gòu)造直方圖,首先將可能的x值范圍分配到通常相等大小和相鄰的區(qū)間。
pyplot.hist函數(shù)定義文檔:plot.hist函數(shù)定義文檔plot.hist(https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html)
現(xiàn)在我們創(chuàng)建一個(gè)隨機(jī)數(shù)的直方圖:
import matplotlib.pyplot as plt import numpy as np gaussian_numbers = np.random.normal(size=10000) print(gaussian_numbers) ''' 輸出結(jié)果: [-2.88618646 -0.15302214 -0.35230715 ... -0.42074156 0.41650123 0.56230326] '''plt.hist(gaussian_numbers) plt.title("Gaussian Histogram") plt.xlabel("Value") plt.ylabel("Frequency") plt.show()
返回值:
?n The values of the histogram bins.
?bins
The edges of the bins. Length nbins + 1 (nbins left edges and right edge of last bin).
?patches
Silent list of individual patches used to create the histogram or list of such list if multiple input datasets.
參數(shù)說(shuō)明:
?data:必選參數(shù),繪圖數(shù)據(jù)
?bins:直方圖的長(zhǎng)條形數(shù)目,可選項(xiàng),默認(rèn)為10
?normed:是否將得到的直方圖向量歸一化,可選項(xiàng),默認(rèn)為0,代表不歸一化,顯示頻數(shù)。normed=1,表示歸一化,顯示頻率。
?facecolor:長(zhǎng)條形的顏色
?edgecolor:長(zhǎng)條形邊框的顏色
?alpha:透明度
讓我們?cè)黾觔in的數(shù)量。 如果有10,000個(gè)隨機(jī)值,將關(guān)鍵字參數(shù)bins設(shè)置為100:
plt.hist(gaussian_numbers, bins=100) plt.show()hist的另一個(gè)重要關(guān)鍵字參數(shù)是density。 density是可選的,默認(rèn)值為False。 如果將其設(shè)置為T(mén)rue,則返回元組的第一個(gè)元素將被歸一化以形成概率密度的計(jì)數(shù)值, 即直方圖下的面積(或積分)總和為1。
plt.hist(gaussian_numbers, bins=100, density=True) plt.show()
hist可設(shè)置edgecolor和color。
通過(guò)設(shè)置參數(shù)cumulative,我們也可以將其繪制為累積分布函數(shù)。
n, bins, patches = plt.hist(gaussian_numbers,bins=100,density=True,edgecolor="#BB5566",color="#DDFFDD",cumulative=True) plt.show()總結(jié)
以上是生活随笔為你收集整理的19、20_散点图、连接散点图、气泡图、不同颜色的散点图、直方图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 特斯拉自动驾驶跟挡风玻璃有关吗?
- 下一篇: 21.等值线图(Counter Plot