您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Python怎么實(shí)現(xiàn)折線圖、柱狀圖、餅圖”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
代碼
import numpy as np import matplotlib.pyplot as plt # x軸刻度標(biāo)簽 x_ticks = ['a', 'b', 'c', 'd', 'e', 'f'] # x軸范圍(0, 1, ..., len(x_ticks)-1) x = np.arange(len(x_ticks)) # 第1條折線數(shù)據(jù) y1 = [5, 3, 2, 4, 1, 6] # 第2條折線數(shù)據(jù) y2 = [3, 1, 6, 5, 2, 4] # 設(shè)置畫布大小 plt.figure(figsize=(10, 6)) # 畫第1條折線,參數(shù)看名字就懂,還可以自定義數(shù)據(jù)點(diǎn)樣式等等。 plt.plot(x, y1, color='#FF0000', label='label1', linewidth=3.0) # 畫第2條折線 plt.plot(x, y2, color='#00FF00', label='label2', linewidth=3.0) # 給第1條折線數(shù)據(jù)點(diǎn)加上數(shù)值,前兩個(gè)參數(shù)是坐標(biāo),第三個(gè)是數(shù)值,ha和va分別是水平和垂直位置(數(shù)據(jù)點(diǎn)相對(duì)數(shù)值)。 for a, b in zip(x, y1): plt.text(a, b, '%d'%b, ha='center', va= 'bottom', fontsize=18) # 給第2條折線數(shù)據(jù)點(diǎn)加上數(shù)值 for a, b in zip(x, y2): plt.text(a, b, '%d'%b, ha='center', va= 'bottom', fontsize=18) # 畫水平橫線,參數(shù)分別表示在y=3,x=0~len(x)-1處畫直線。 plt.hlines(3, 0, len(x)-1, colors = "#000000", linestyles = "dashed") # 添加x軸和y軸刻度標(biāo)簽 plt.xticks([r for r in x], x_ticks, fontsize=18, rotation=20) plt.yticks(fontsize=18) # 添加x軸和y軸標(biāo)簽 plt.xlabel(u'x_label', fontsize=18) plt.ylabel(u'y_label', fontsize=18) # 標(biāo)題 plt.title(u'Title', fontsize=18) # 圖例 plt.legend(fontsize=18) # 保存圖片 plt.savefig('./figure.pdf', bbox_inches='tight') # 顯示圖片 plt.show()
效果
代碼
import numpy as np import matplotlib.pyplot as plt # x軸刻度標(biāo)簽 x_ticks = ['a', 'b', 'c', 'd', 'e', 'f'] # 柱的寬度 barWidth = 0.25 # 第1個(gè)柱的x軸范圍(每個(gè)柱子的中點(diǎn))(0, 1, ..., len(x_ticks)-1) x1 = np.arange(len(x_ticks)) # 第2個(gè)柱的x軸范圍(每個(gè)柱子的中點(diǎn)) x2 = [x + barWidth for x in x1] # 第1個(gè)柱數(shù)據(jù) y1 = [5, 3, 2, 4, 1, 6] # 第2個(gè)柱數(shù)據(jù) y2 = [3, 1, 6, 5, 2, 4] # 設(shè)置畫布大小 plt.figure(figsize=(10, 6)) # 畫第1個(gè)柱 plt.bar(x1, y1, color='#FF0000', width=barWidth, label='label1') # 畫第2個(gè)柱 plt.bar(x2, y2, color='#00FF00', width=barWidth, label='label2') # 給第1個(gè)柱數(shù)據(jù)點(diǎn)加上數(shù)值,前兩個(gè)參數(shù)是坐標(biāo),第三個(gè)是數(shù)值,ha和va分別是水平和垂直位置(數(shù)據(jù)點(diǎn)相對(duì)數(shù)值)。 for a, b in zip(x1, y1): plt.text(a, b, '%d'%b, ha='center', va= 'bottom', fontsize=18) # 給第2個(gè)柱數(shù)據(jù)點(diǎn)加上數(shù)值 for a, b in zip(x2, y2): plt.text(a, b, '%d'%b, ha='center', va= 'bottom', fontsize=18) # 畫水平橫線 plt.hlines(3, 0, len(x_ticks)-1+barWidth, colors = "#000000", linestyles = "dashed") # 添加x軸和y軸刻度標(biāo)簽 plt.xticks([r + barWidth/2 for r in x1], x_ticks, fontsize=18) plt.yticks(fontsize=18) # 添加x軸和y軸標(biāo)簽 plt.xlabel(u'x_label', fontsize=18) plt.ylabel(u'y_label', fontsize=18) # 標(biāo)題 plt.title(u'Title', fontsize=18) # 圖例 plt.legend(fontsize=18) # 保存圖片 plt.savefig('./figure.pdf', bbox_inches='tight') # 顯示圖片 plt.show()
效果
代碼
import numpy as np import matplotlib.pyplot as plt # 設(shè)置畫布大小 plt.figure(figsize=(10, 10)) # 設(shè)置每塊區(qū)域的標(biāo)簽 labels = ['a', 'b', 'c', 'd', 'e'] # 設(shè)置每塊區(qū)域離圓心的距離,這里a區(qū)域凸出一點(diǎn)點(diǎn) explode = [0.05, 0.01, 0.01, 0.01, 0.01] # 設(shè)置每塊區(qū)域的值 values = [1, 5, 2, 4, 3] # 設(shè)置每塊區(qū)域的顏色 colors = ['#F5DEB3', '#87CEFA', '#FFB6C1', '#90EE90', '#D3D3D3'] _, l_text, p_text = plt.pie(values, explode=explode, labels=labels, autopct='%1.1f%%', colors=colors) # 設(shè)置標(biāo)簽字體大小 for t in l_text: t.set_size(18) # 設(shè)置數(shù)值字體大小 for t in p_text: t.set_size(18) # 標(biāo)題 plt.title(u'Title', fontsize=18) # 圖例 plt.legend(fontsize=18) # 保存圖片 plt.savefig('./figure.pdf', bbox_inches='tight') # 顯示圖片 plt.show()
“Python怎么實(shí)現(xiàn)折線圖、柱狀圖、餅圖”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。