1. 程式人生 > 資訊 >4999 元起,小米 MIX 4 陶瓷白明日 10 點全渠道開售:搭載驍龍 888+,屏下攝像頭

4999 元起,小米 MIX 4 陶瓷白明日 10 點全渠道開售:搭載驍龍 888+,屏下攝像頭

目錄

matplotlib 是一個2D繪相簿。

  • 折線圖: 能夠顯示資料的變化趨勢,反映事物的變化情況(變化)

  • 散點圖:判斷變數之間是否存在數量關聯趨勢,展示離群點(分佈規律)

  • 柱狀圖: 繪製離散額資料,比較資料之間的差別(統計/對比)

  • 直方圖:繪製連續性的資料,展示一組或多組資料的分佈狀況(統計)

  • 餅圖:分類資料的佔比情況(佔比)


import matplotlib.pyplot as plt

%matplotlib inline  #在jupyter中顯示

plt.show()

折線圖

# 對摺線進行操作
plt.plot(x,y.color='red',alpha=1,linestyle='-',linewidth=3)
'''
color: 設定顏色,跟單詞或16進位制的色值
alpha: 設定透明度,0-1. 0:透明。 1:不透明
linestyle: 設定線的形狀。 '--' 虛線。 '-' 實線。 '-.' :
linewidth: 設定寬度
'''

# 對摺點進行操作
plt.plot(x,y,marker = 'o',markersize =10, markerfacecolor = 'red', markeredgecolor = 'black', markeredgewidth =5)
'''
marker 折點的形狀
markersize 折點大小
markerfacecolor 折點顏色
markeredgecolor 折點邊緣顏色
markeredgewidth 折點邊緣寬度
'''

# 設定圖片大小與儲存
plt.figure(figsize = (20,8),dpi =80) # 長寬大小為英寸,dpi畫素
plt.savefig(./名稱.png)  # png為向量圖形,可進行摳圖
plt.show()  # 儲存應該在show之前


# 構造 x,y軸
x_ticks_label = ["{}:00".format(i) for i in range(min(x),max(x)+1]  #設定x軸標籤
plt.xticks(x[::3],x_ticks_lable[::3],rotation = 45)  #rotation 標籤旋轉角度,[::3]選取顯示的刻度



# 修改座標軸範圍
x = np.arrange(-10,11,1)
y = x**2
plt.plot(x,y)
plt.xlim([-5,5])   # .xlim 可修改範圍,也可以只修改單邊資料 plt.xlim(xmin =-4)
plt.ylim([0,80])

# 座標軸顯示
ax = plt.gca()    # 顯示邊緣線
ax.spines['right'].set_color('none')  #對相應的邊進行修改 

# 設定原點
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))

# 設定中文字型
from matplotlib import font_manager
my_font  = font_manager.FontProperties(fname = '字型路徑',size = 10)  # "C:\Windows\Fonts"
plt.title("標題名稱",fontproperties = my_font, color = '', size = )


# 一圖多線

y1 = [1,22,33,44,55]
y2 = [3,45,67,67,78]
x = range(1,6)

plt.plot(x,y1,label = 'y1',color = '',zorder = 20) # zoeder誰在圖層上方
plt.plot(x,y2,label = 'y2',color ='',zorder = 2)
plt.legend(prop = my_font,loc = 'upper right') #圖例
plt.grid(alpha = 1) # 繪製網格


# 多個座標系子圖
import numpy as np
x = np.arange(1,100)
fig = plt.figure(figsize=(20,8),dpi = 80) #繪製畫布
ax1 =fig.add_subplot(2,2,1)    # 將畫布進行分割,將橫向分為兩部分,將縱向分為兩部分
ax1.plot(x,x)
ax2 = fig.add_subplot(2,2,2)
ax2.plot(x,x**2)
ax3 = fig.add_subplot(2,2,3)
ax3.plot(x,np.log(x))
ax4 = fig.add_subplot(2,2,4)
ax1.plot(x,x)

散點圖

fig = plt.figure(figsize=(20,8),dpi = 80) #繪製畫布
x= range(1,8)
y = [1,2,34,45,6,7,78]
plt.scatter(x,y)    # scatter 繪製散點圖

條形圖

a= ['a','b','c','d','e']
b= ['33.09','44.88','34.67','56','75']
fig = plt.figure(figsize=(20,8),dpi = 80) #繪製畫布

reacts = plt.bar(range(len(a)),[float(i) for i in b, width = 0.3, color['r','g','b']]  # bar 條形圖
plt.xticks(range(len(a)),a,fontproperties = my_font)

for react in reacts:             # 在條形圖上寫上資料的大小
    height = react.get_height()
    plt.text(react.get_x()+react.get_width()/2,height+0.5,str(height),ha='center') # 顯示資料大小的“x座標,y座標,大小”

# 橫向條形圖
reacts = plt.barh(range(len(a)),[float(i) for i in b, width = 0.3, color['r','g','b']]  # barh  橫向條形圖
plt.yticks(range(len(a)),a,fontproperties = my_font)

for react in reacts:             # 在條形圖上寫上資料的大小
    width = react.get_width()
    plt.text(width,react.gety()+0.15,str(width),va='center') # 顯示資料大小的“x座標,y座標,大小”

# 並列條形圖與羅列條形圖
index = np.arange(4)
BJ =[50,55,54,60]
SH = [44,65,42,67]

# 並列條形圖
plt.bar(index,BJ,width=0.3)
plt.xticks(index+0.3/2,index) #將zuo'biao刻度放在兩柱形中間
plt.bar(index + 0.3,SH,width = 0.3)

# 羅列條形圖,堆積條形圖
plt.bar(index,BJ,width=0.3)
plt.xticks(index+0.3/2,index) #將zuo'biao刻度放在兩柱形中間
plt.bar(index + 0.3,SH,bottom =BJ,width = 0.3)  #設定bottom

直方圖

time = [1,2,3,4,5,6,7,8,9,0,9,8,7,6,5,4,3,2,1,3,5,6,7,8,5,3,5,7,8,3,4,5,6,8,5]
fig = plt.figure(figsize=(20,8),dpi = 80) #繪製畫布
distance = 2
group_num = int((max(time)-min(time))/distance)

plt.hist(time,bins = group_num) # bins 分組

plt.xticks(range(min(time),max(time))[::2])
plt.grid(linestyle = '--',alpha = 0.5 )

餅圖

# 標籤
label_list = ['第一部分','第二部分','第三部分']
# 資料
size = [55,35,10]
# 突出顯示
explode = [0,0.05,0]
# 顏色
color = ['r','g','b']
fig = plt.figure(figsize=(20,8),dpi = 80) #繪製畫布
pathes,l_text,p_text = plt.pie(size,explode = explode,colors = color,label = label_list,labeldistance =1.1,autopct='%1.1f%%',shadow =False)  # 對餅狀圖的資料進行接收

'''
size 大小,資料
explode : ~~是否顯示突出
colors :顏色
labels:標籤
labeldistance 標籤距離圓心位置
autopct: 資料顯示形式
shadow : 陰影
pathes: 扇形列表
l_text: 標籤列表
p_text: 扇形百分比列表

'''
for t in l_text:     
    t.set_fontproperties(my_font)
for i in p_text:
    i.set_size(17)
for j in pathes:
    j.set_color('pink')
    break