目錄
前言
本章節講的是關於軸的設置,盡可能舉例多種情況。
(一)設置軸的范圍
1.同時對於x,y軸設置
(1)語法說明
plt.axis([xmin, xmax, ymin, ymax])
(2)源代碼
# 導入模塊
import matplotlib.pyplot as plt
import numpy as np
# 數據
x = np.linspace(-10, 10, 100)
y = x**2
# 繪圖
plt.plot(x, y)
# 設置軸的范圍
plt.axis([-6, 7, -1, 30])
# 展示
plt.show()
(3)輸出效果
2.分別對與x,y軸的設置
(1)語法說明
- 對於x軸:plt.xlim(start, end)
- 對於y軸:plt.ylim(start, end)
如果是使用ax對象設置范圍的話,則可在前加set_命令
ax.set_xlim(start, end), ax.set_ylim(start, end), 其他的命令類似如此。
(2)源代碼
# 導入模塊
import matplotlib.pyplot as plt
import numpy as np
# 數據
x = np.linspace(-10, 10, 100)
y = x**2
# 繪圖
plt.plot(x, y)
# 設置軸的范圍
plt.xlim(-3, 8)
plt.ylim(-2, 50)
# 展示
plt.show()
(3)輸出效果
(二)設置刻度的大小
1.普通的刻度設置
(1)說明
x軸的刻度:plt.xticks(item)
y軸的刻度:plt.yticks(item)
(2)源代碼
# 導入模塊
import matplotlib.pyplot as plt
import numpy as np
# 數據
x = np.linspace(-10, 10, 100)
y = x**2
# 繪圖
plt.plot(x, y)
# 設置軸的刻度
plt.xticks(range(-8, 8, 2))
plt.yticks([0, -3, -6, 7, 15, 20, 37, 48, 72])
# 展示
plt.show()
(3)輸出效果
2.添加文本的刻度設置
(1)說明
其實就是在設置刻度的基礎上,在添加一個列表,來顯示刻度。
如:plt.xticks(['數據'], ["標簽"])
(2)源代碼
# 導入模塊
import matplotlib.pyplot as plt
import numpy as np
# 數據
x = np.linspace(-10, 10, 100)
y = x**2
# 繪圖
plt.plot(x, y)
# 設置軸的刻度
plt.xticks(range(-8, 8, 2))
plt.yticks([0, 40, 60], ["bad", 'good', "best"])
# 展示
plt.show()
(3)輸出效果
3.主副刻度的設置
(1)說明
- 需要導入:from matplotlib.ticker import MultipleLocator, FormatStrFormatter 模塊
- 主刻度:(y軸同理)
- 倍數:ax.xaxis.set_major_locator(MultipleLocator(倍數))
- 文本格式:ax.xaxis.set_major_formatter(FormatStrFormatter('%占位數.小數點數f'))
- 副刻度:(將"major"改為"minor"即可)
- 倍數:ax.xaxis.set_minor_locator(MultipleLocator(倍數))
- 文本格式:ax.xaxis.set_minor_formatter(FormatStrFormatter('%占位數.小數點數f'))
(2)源代碼
# 導入模塊
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
import numpy as np
# 數據
x = np.linspace(-30, 30, 100)
y = x**2
# 繪圖
plt.plot(x, y)
ax = plt.gca()
# 設置軸的主刻度
# x軸
ax.xaxis.set_major_locator(MultipleLocator(20)) # 設置20倍數
ax.xaxis.set_major_formatter(FormatStrFormatter('%5.1f')) # 設置文本格式
# y軸
ax.yaxis.set_major_locator(MultipleLocator(100)) # 設置100倍數
ax.yaxis.set_major_formatter(FormatStrFormatter('%1.2f')) # 設置文本格式
# 設置軸的副刻度
# x軸
ax.xaxis.set_minor_locator(MultipleLocator(5)) # 設置10倍數
# ax.xaxis.set_minor_formatter(FormatStrFormatter('%2.1f')) # 設置文本格式
# y軸
ax.yaxis.set_minor_locator(MultipleLocator(50)) # 設置50倍數
# ax.yaxis.set_minor_formatter(FormatStrFormatter('%1.0f')) # 設置文本格式
# 設置網格
ax.xaxis.grid(True, which='major') # x坐標軸的網格使用主刻度
ax.yaxis.grid(True, which='minor') # y坐標軸的網格使用次刻度
# 展示
plt.show()
(3)輸出效果
(三)設置軸的數據
1.說明:
以x軸的數為日期,再以plt.gcf().autofmt_xdate()來旋轉顯示日期數據。
2.源代碼
# 導入模塊
import matplotlib.pyplot as plt
import numpy as np
# 數據
N = 4
y = np.random.randint(-20, 20, (1, N)).flatten()
x = ["2019-3-13", "2019-3-14", "2019-3-15", "2019-3-16"]
# 繪圖
plt.plot(x, y)
# 旋轉日期顯示
plt.gcf().autofmt_xdate()
# 展示
plt.show()
3.輸出效果:
(四)設置axes脊柱
1.屬性列表
ax.spines[' '].xxx | 說明 | 默認值 |
---|---|---|
set_visible(bool) | 邊框的可見性 | True |
ax.xaxis.set_ticks_position({"top","left"……}) | 刻度的顯示位置 | 外面(不是ax.spines[' '].) |
set_position({"top","left"……}) | 邊框的位置 | 左下角為交點 |
set_color(string) | 邊框的顏色 | “black"(當值為None也是隱藏) |
set_linewidth(int) | 邊框的寬度 | 1 |
set_linestyle(string) | 邊框的線性 | ”-“ |
2.實例1-修改默認的坐標樣式
(1).說明:
設置反方向(y軸同理):
x軸反向:ax.invert_xaxis()
(2).源代碼:
# 導入模塊
import matplotlib.pyplot as plt
import numpy as np
# 數據
x = np.linspace(-10, 10, 100)
y = x**2
# 繪圖
plt.plot(x, y)
ax = plt.gca()
# ===設置脊(邊框)===
# 1.隱藏上與右的邊框
ax.spines['top'].set_visible(False)
ax.spines['right'].set_color(None)
# 2.設置顏色
ax.spines['left'].set_color('b')
ax.spines['bottom'].set_color('r')
# 3.設置線寬
ax.spines['left'].set_linewidth(5)
ax.spines['bottom'].set_linewidth(3)
# 4.設置線形
ax.spines['left'].set_linestyle('--')
ax.spines['left'].set_linestyle('-.')
# 5.設置交點位置(0, 35)
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 35))
# 6.設置數據顯示的位置
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('right')
# 7.設置反方向(y軸同理)
ax.invert_xaxis() # x軸反向
# 展示
plt.show()
(3).輸出效果:
3.實例2-帶箭頭的坐標系
(1)說明:
需要導入:import mpl_toolkits.axisartist as axisartist
大致步驟如下:
- 隱藏原有的邊框坐標系
- 創建新的坐標系
- 添加箭頭
注意:再創建新的坐標系時ax.new_floating_axis(0, 0)
- 第一個參數:0表示橫線,1表示豎線
- 第二格參數:表示經過那個坐標點。
(2)源代碼:
# 導入模塊
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
import numpy as np
# 數據
x = np.linspace(-10, 10, 100)
y = x**2
# 創建畫布
fig = plt.figure(figsize=(4, 6))
ax = axisartist.Subplot(fig, 111)
# 將繪圖區對象添加到畫布中
fig.add_axes(ax)
# ===帶箭頭坐標系的設置===
# 1.隱藏原有的邊框坐標系
ax.axis[:].set_visible(False)
# 2.創建新的坐標系
ax.axis["x"] = ax.new_floating_axis(0, 0)
ax.axis["y"] = ax.new_floating_axis(1, 0)
# 3.添加箭頭
ax.axis["x"].set_axisline_style("->", size=2.0)
ax.axis["y"].set_axisline_style("->", size=1.0)
# 繪圖
ax.plot(x, y)
# 展示
plt.show()