1. 程式人生 > >Matplotlib資料視覺化——設定座標軸,原點,圖例

Matplotlib資料視覺化——設定座標軸,原點,圖例

今天突破了導軌和轉檯的除錯程式,學習會Python娛樂一下
matplotlib庫和MATLAB的資料視覺化功能幾乎是相同,只是因為人工智慧的火爆以及使用方便深受大家喜愛。

1.基本使用

1.1figure影象

繪製單一函式的影象

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 1, 30)
y = 3*x + 1
plt.plot(x, y)
plt.show()

在這裡插入圖片描述
繪製兩個函式

import matplotlib.pyplot as plt
import numpy as
np x = np.linspace(-3, 3, 50) # 注意linspace不是linespace y1 = 2*x+1 y2 = x**2+1 # 乘方注意和C的區別 # 繪製第一個函式 plt.figure() plt.plot(x, y1) # 繪製第二個函式 plt.figure(num=3, figsize=(6, 6)) plt.plot(x, y2) plt.show()

在這裡插入圖片描述
在這裡插入圖片描述
繪製在同一座標系中

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-3,3,50)
y1 =
2*x+1 y2 = x**2 #繪製在同一個figure中 plt.figure() plt.plot(x, y1) plt.plot(x, y2, color='violet', linewidth=3.0, linestyle='--') # 指定顏色,線寬和線型 plt.show()

在這裡插入圖片描述

1.2 座標軸的設定

  • 擷取x,y的某一區間進行展示
  • 設定x,y的標籤
  • 設定某一軸的刻度間隔劃分
  • 設定座標值對應的相應含義並正則化設定字型格式
import matplotlib.pyplot as plt
import numpy as np
x =
np.linspace(-3,3,50) y1 = 2*x+1 y2 = x**2 #繪製在同一個figure中 plt.figure() plt.plot(x,y1) plt.plot(x,y2,color='red',linewidth = 2.0,linestyle = '--')#指定顏色,線寬和線型 #擷取x,y的某一部分 plt.xlim((-1,2)) plt.ylim((-2,3)) #設定x,y的座標描述標籤 plt.xlabel("I am x") plt.ylabel("I am y") #設定x刻度的間隔 new_ticks = np.linspace(-1,2,5) plt.xticks(new_ticks) plt.yticks([-2, -1.5, 0, 1.5, 3], [r'$Really\ bad\ \alpha$', r'$bad$', r'$normal$', r'$good$', r'$very\ good$'])#r表示正則化,$$表示用數學字型輸出 plt.show()

在這裡插入圖片描述

  • 移動座標軸的位置
  • 隱藏上下左右的某一個軸
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-3,3,50)
y1 = 2*x+1
y2 = x**2

#繪製在同一個figure中
plt.figure()
plt.plot(x,y1)
plt.plot(x,y2,color='red',linewidth = 2.0,linestyle = '--')#指定顏色,線寬和線型

#擷取x,y的某一部分
plt.xlim((-1,2))
plt.ylim((-2,3))
#設定x,y的座標描述標籤
plt.xlabel("I am x")
plt.ylabel("I am y")
#設定x刻度的間隔
new_ticks = np.linspace(-1,2,5)
plt.xticks(new_ticks)
plt.yticks([-2, -1.5, 0, 1.5, 3],
           [r'$Really\ bad\ \alpha$', r'$bad$',
            r'$normal$', r'$good$', r'$very\ good$'])
           #r表示正則化,$$表示用數學字型輸出
# gca = 'get current axis'
ax = plt.gca()#獲取當前座標的位置
#去掉座標圖的上和右 spine翻譯成脊樑
ax.spines['right'].set_color('None')
ax.spines['top'].set_color('None')
#指定座標的位置
ax.xaxis.set_ticks_position('bottom') # 設定bottom為x軸
ax.yaxis.set_ticks_position('left') # 設定left為x軸
ax.spines['bottom'].set_position(('data',0))#這個位置的括號要注意
ax.spines['left'].set_position(('data',0))
plt.show()

在這裡插入圖片描述

1.3設定legend圖例

最簡單的方法就是在plot中打上label然後用legend( )方法實現

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-3,3,50)
y1 = 2*x+1
y2 = x**2

#繪製在同一個figure中
plt.figure()
plt.plot(x,y1,label='山高月小')
plt.plot(x,y2,color='red',linewidth = 2.0,linestyle = '--',label='水落石出')#指定顏色,線寬和線型

plt.legend()
plt.show()

在這裡插入圖片描述
如圖所示,中文出現亂碼,上網查了一下,需要在開頭加上

plt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標籤
plt.rcParams['axes.unicode_minus']=False #用來正常顯示負號

這時顯示正常了
在這裡插入圖片描述
更加高階的方法

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標籤
plt.rcParams['axes.unicode_minus']=False #用來正常顯示負號

x = np.linspace(-3,3,50)
y1 = 2*x+1
y2 = x**2

#繪製在同一個figure中
plt.figure()
l1, = plt.plot(x,y1,label='山高月小')#放在handles中l1一定要加,
l2, = plt.plot(x,y2,color='red',linewidth = 2.0,linestyle = '--',label='水落石出')

plt.legend(handles=[l1, l2,],labels=['小舟從此逝','江海寄餘生'],loc='best')
plt.show()

在這裡插入圖片描述
如果只打印l1的圖例,則修改

plt.legend(handles=[l1,],labels=['小舟從此逝',],loc='best')

在這裡插入圖片描述
未完待續~~