本文的文字及圖片來源於網絡,僅供學習、交流使用,不具備任何商業用途,若有問題請及時聯繫咱們以做處理。網絡
Python爬蟲、數據分析、網站開發等案例教程視頻免費在線觀看ide
https://space.bilibili.com/523606542
有不少很牛b的做圖教程,我也學不來,就扔給你們本身學吧:學習
折線圖代碼字體
import numpy as npimport matplotlib.pyplot as plt# x軸刻度標籤x_ticks = ['a', 'b', 'c', 'd', 'e', 'f']# x軸範圍(0, 1, ..., len(x_ticks)-1)x = np.arange(len(x_ticks))# 第1條折線數據y1 = [5, 3, 2, 4, 1, 6]# 第2條折線數據y2 = [3, 1, 6, 5, 2, 4]# 設置畫布大小plt.figure(figsize=(10, 6))# 畫第1條折線,參數看名字就懂,還能夠自定義數據點樣式等等。plt.plot(x, y1, color='#FF0000', label='label1', linewidth=3.0)# 畫第2條折線plt.plot(x, y2, color='#00FF00', label='label2', linewidth=3.0)# 給第1條折線數據點加上數值,前兩個參數是座標,第三個是數值,ha和va分別是水平和垂直位置(數據點相對數值)。for a, b in zip(x, y1): plt.text(a, b, '%d'%b, ha='center', va= 'bottom', fontsize=18)# 給第2條折線數據點加上數值for a, b in zip(x, y2): plt.text(a, b, '%d'%b, ha='center', va= 'bottom', fontsize=18)# 畫水平橫線,參數分別表示在y=3,x=0~len(x)-1處畫直線。plt.hlines(3, 0, len(x)-1, colors = "#000000", linestyles = "dashed")# 添加x軸和y軸刻度標籤plt.xticks([r for r in x], x_ticks, fontsize=18, rotation=20) plt.yticks(fontsize=18)# 添加x軸和y軸標籤plt.xlabel(u'x_label', fontsize=18) plt.ylabel(u'y_label', fontsize=18)# 標題plt.title(u'Title', fontsize=18)# 圖例plt.legend(fontsize=18)# 保存圖片plt.savefig('./figure.pdf', bbox_inches='tight')# 顯示圖片plt.show()
效果網站
柱狀圖
代碼spa
import numpy as npimport matplotlib.pyplot as plt# x軸刻度標籤x_ticks = ['a', 'b', 'c', 'd', 'e', 'f']# 柱的寬度barWidth = 0.25# 第1個柱的x軸範圍(每一個柱子的中點)(0, 1, ..., len(x_ticks)-1)x1 = np.arange(len(x_ticks))# 第2個柱的x軸範圍(每一個柱子的中點)x2 = [x + barWidth for x in x1]# 第1個柱數據y1 = [5, 3, 2, 4, 1, 6]# 第2個柱數據y2 = [3, 1, 6, 5, 2, 4]# 設置畫布大小plt.figure(figsize=(10, 6))# 畫第1個柱plt.bar(x1, y1, color='#FF0000', width=barWidth, label='label1')# 畫第2個柱plt.bar(x2, y2, color='#00FF00', width=barWidth, label='label2')# 給第1個柱數據點加上數值,前兩個參數是座標,第三個是數值,ha和va分別是水平和垂直位置(數據點相對數值)。for a, b in zip(x1, y1): plt.text(a, b, '%d'%b, ha='center', va= 'bottom', fontsize=18)# 給第2個柱數據點加上數值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軸刻度標籤plt.xticks([r + barWidth/2 for r in x1], x_ticks, fontsize=18) plt.yticks(fontsize=18)# 添加x軸和y軸標籤plt.xlabel(u'x_label', fontsize=18) plt.ylabel(u'y_label', fontsize=18)# 標題plt.title(u'Title', fontsize=18)# 圖例plt.legend(fontsize=18)# 保存圖片plt.savefig('./figure.pdf', bbox_inches='tight')# 顯示圖片plt.show()
效果視頻
餅圖
代碼blog
import numpy as npimport matplotlib.pyplot as plt# 設置畫布大小plt.figure(figsize=(10, 10))# 設置每塊區域的標籤labels = ['a', 'b', 'c', 'd', 'e']# 設置每塊區域離圓心的距離,這裏a區域凸出一點點explode = [0.05, 0.01, 0.01, 0.01, 0.01]# 設置每塊區域的值values = [1, 5, 2, 4, 3]# 設置每塊區域的顏色colors = ['#F5DEB3', '#87CEFA', '#FFB6C1', '#90EE90', '#D3D3D3'] _, l_text, p_text = plt.pie(values, explode=explode, labels=labels, autopct='%1.1f%%', colors=colors)# 設置標籤字體大小for t in l_text: t.set_size(18)# 設置數值字體大小for t in p_text: t.set_size(18)# 標題plt.title(u'Title', fontsize=18)# 圖例plt.legend(fontsize=18)# 保存圖片plt.savefig('./figure.pdf', bbox_inches='tight')# 顯示圖片plt.show()
效果教程