close

背景介紹

今天我們來學習一下,使用Python如何繪製常見的二維圖形。

教程代碼可直接複製,粘貼到python的IDE中進行運行即可出圖。

軟件介紹

[軟件名稱]:Anaconda | Spyder

[軟件安裝]:可以參考下方這個推文

Anaconda的下載和安裝

基礎繪圖折線圖# 導入庫import matplotlib.pyplot as plt# 設置數據date = [0,3,7,10,15,20,21,25,26,30]body_temp = [36.8, 37.2, 37.6,37.8,38.5, 38.2, 38.0, 37.5, 37.6, 36.8]# 繪製折線圖plt.plot(date,body_temp, # 設置顏色 color = "r", # 設置線型 linestyle = "--", # 設置標記 marker = "o")# 設置x標籤plt.xlabel("Date (day)")# 設置y標籤plt.ylabel("Body temperature (℃)")# 設置標題plt.title("The change of body temperature")# 設置x軸的標籤plt.xticks([0,10,20,30])# 設置y軸的標籤plt.yticks([36,37,38,39])plt.show()

多條折線# 導入庫import matplotlib.pyplot as plt# 設置數據date = [0,3,7,10,15,20,21,25,26,30]body_temp = [36.8, 37.2, 37.6,37.8,38.5, 38.2, 38.0, 37.5, 37.6, 36.8]body_temp2 = [36.3, 36.8, 36.9, 37.1,37.5, 38.2, 37.9, 37.8, 37.7, 36.4]# 繪製第一條線plt.plot(date,body_temp, color = "r", marker = "+", label = "Line1")# 繪製第二條線plt.plot(date,body_temp2, color = "g", marker = "^", label = "Line2")plt.xlabel("Date (day)")plt.ylabel("Body temperature (℃)")plt.title("The change of body temperature")plt.legend()

散點圖# 導入庫import matplotlib.pyplot as plt# 設置數據date = [0,3,7,10,15,20,21,25,26,30]body_temp = [36.8, 37.2, 37.6,37.8,38.5, 38.2, 38.0, 37.5, 37.6, 36.8]plt.scatter(date,body_temp, color = "r")plt.show()

兩個散點圖# 導入庫import matplotlib.pyplot as plt# 導入庫import matplotlib.pyplot as plt# 設置數據date = [0,3,7,10,15,20,21,25,26,30]body_temp = [36.8, 37.2, 37.6,37.8,38.5, 38.2, 38.0, 37.5, 37.6, 36.8]body_temp2 = [36.3, 36.8, 36.9, 37.1,37.5, 38.2, 37.9, 37.8, 37.7, 36.4]# 繪製第一條線plt.scatter(date,body_temp, color = "r", marker = "+", label = "Line1")# 繪製第二條線plt.scatter(date,body_temp2, color = "g", marker = "^", label = "Line2")plt.xlabel("Date (day)")plt.ylabel("Body temperature (℃)")plt.title("The change of body temperature")plt.legend()

柱狀圖# 導入庫import matplotlib.pyplot as plt# 設置數據date = [0,1,2,3,4,5,6,7,8,9]body_temp = [36.8, 37.2, 37.6,37.8,38.5, 38.2, 38.0, 37.5, 37.6, 36.8]plt.bar(date,body_temp, color = "r")plt.ylim(36,39)plt.xlabel("Date (day)")plt.ylabel("Body temperature (℃)")plt.title("The change of body temperature")plt.legend()

色彩斑斕的柱狀圖

通過給與三個或多個顏色連續性填充

# 導入庫import matplotlib.pyplot as plt# 設置數據date = [0,1,2,3,4,5,6,7,8,9]body_temp = [36.8, 37.2, 37.6,37.8,38.5, 38.2, 38.0, 37.5, 37.6, 36.8]plt.bar(date,body_temp, # 填充三種顏色 color = ["r","g","b"], # 填充形狀 hatch = ["/"])plt.ylim(36,39)plt.xlabel("Date (day)")plt.ylabel("Body temperature (℃)")plt.title("The change of body temperature")plt.legend()

其他的填充形狀

'/', '\', '|', '-', '+', 'x','o', 'O', '.', '*'

餅圖# 導入庫import matplotlib.pyplot as plt# 設置數據value = [100,200,300,400,500]label = ["a","b","c","d","e"]# 繪圖plt.pie(x = value,labels = label, # 設置顏色 colors = ['y','g','c','gray',"b"], # 添加陰影 shadow = True, # 添加百分比 autopct = "%1.1f%%", # 設置標籤距離 pctdistance = 0.8, # 設置角度 startangle = 90)

空心餅圖# 導入庫import matplotlib.pyplot as plt# 設置數據value = [100,200,300,400,500]label = ["a","b","c","d","e"]# 繪圖plt.pie(x = value,labels = label, colors = ['y','g','c','gray',"b"], autopct = "%1.1f%%", pctdistance = 0.8, startangle = 90, # 設置空心距離 wedgeprops = dict(width = 0.5))

箱線圖/小提琴圖# 導入庫import matplotlib.pyplot as plt# 設置數據data1 = [8,3,19,12,14,56,40,36,27,10,11]data2 = [1,3,15,12,15,30,13,16,27,10,11]data3 = [8,3,10,12,14,56,67,36,27,10,100]#箱子圖plt.boxplot([data1,data2,data3], labels = ["data1","data2","data3"])#小提琴圖plt.violinplot([data1,data2,data3])

常見的圖可能就這些了,沒有介紹的,之後再介紹一下!

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 鑽石舞台 的頭像
    鑽石舞台

    鑽石舞台

    鑽石舞台 發表在 痞客邦 留言(0) 人氣()