1. 繪制圖表組成元素的主要函數
1.1 plot()——展現量的變化趨勢
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Qt5Agg')
x = np.linspace(0.05, 10, 1000)
y = np.cos(x)
plt.plot(x, y, ls="-", lw=2, label="plot figure")
plt.legend()
plt.show()

1.2 scatter()——尋找變量之間的關系
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Qt5Agg')
x = np.linspace(0.05, 10, 1000)
y = np.random.rand(1000)
plt.scatter(x, y, label="scatter figure")
plt.legend()
plt.show()

1.3 xlim()——設置x軸的數值顯示范圍
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Qt5Agg')
x = np.linspace(0.05, 10, 1000)
y = np.random.rand(1000)
plt.scatter(x, y, label="scatter figure")
plt.legend()
plt.xlim(0.05, 10)
plt.ylim(0, 1)
plt.show()

1.4 xlabel()——設置x軸的標簽文本
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Qt5Agg')
x = np.linspace(0.05, 10, 1000)
y = np.sin(x)
plt.plot(x, y, ls="--", lw=2, c="c", label="plot figure")
plt.legend()
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()

1.5 grid()——繪制刻度線的網格線
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Qt5Agg')
x = np.linspace(0.05, 10, 1000)
y = np.sin(x)
plt.plot(x, y, ls="-.", lw=2, c="c", label="plot figure")
plt.legend()
plt.grid(linestyle=":", color="r")
plt.show()

grid()
函數的主要參數為grid(b, which, axis, color, linestyle, linewidth, **kwargs)
:
b
:布爾值。就是是否顯示網格線的意思。官網說如果b
設置為None
, 且kwargs
長度為0
,則切換網格狀態which
:取值為major
,minor
,both
。 默認為major
axis
:取值為both
,x
,y
。就是想繪制哪個方向的網格線color
:這就不用多說了,就是設置網格線的顏色。或者直接用c
來代替color
也可以linestyle
:也可以用ls
來代替linestyle
, 設置網格線的風格,是連續實線,虛線或者其它不同的線條
1.6 axhline()——繪制平行於x軸的水平參考線
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Qt5Agg')
x = np.linspace(0.05, 10, 1000)
y = np.sin(x)
plt.plot(x, y, ls="-.", lw=2, c="c", label="plot figure")
plt.legend()
plt.axhline(y=0.0, c="r", ls="--", lw=2)
plt.axvline(x=4.0, c="r", ls="--", lw=2)
plt.show()

1.7 axvspan()——繪制垂直於x軸的參考區域
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Qt5Agg')
x = np.linspace(0.05, 10, 1000)
y = np.sin(x)
plt.plot(x, y, ls="-.", lw=2, c="c", label="plot figure")
plt.legend()
plt.axvspan(xmin=4.0, xmax=6.0, facecolor="y", alpha=0.3)
plt.axhspan(ymin=0.0, ymax=0.5, facecolor="y", alpha=0.3)
plt.show()

1.8 annotate()——添加圖形內容細節的指向型注釋文本
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Qt5Agg')
x = np.linspace(0.05, 10, 1000)
y = np.sin(x)
plt.plot(x, y, ls="-.", lw=2, c="c", label="plot figure")
plt.legend()
plt.annotate(s="maximum",
xy=(np.pi / 2, 1.0),
xytext=((np.pi / 2) + 1.0, 0.8),
weight="bold",
color="b",
arrowprops=dict(arrowstyle="->", connectionstyle="arc3", color="b")
)
plt.show()
xy
:被注釋圖形內容的位置坐標
xytext
:注釋文本的位置坐標
weight
:注釋文本的字體粗細風格
color
:注釋文本的字體顏色
arrowprops
:指示被注釋內容的箭頭的屬性字典

1.9 text()——添加圖形內容細節的無指向型注釋文本
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Qt5Agg')
x = np.linspace(0.05, 10, 1000)
y = np.sin(x)
plt.plot(x, y, ls="-.", lw=2, c="c", label="plot figure")
plt.legend()
plt.text(x=3.10, y=0.09, s="y=sin(x)", weight="bold", color="b")
plt.show()

1.10 title()——添加圖形內容的標題
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
x = np.linspace(-2, 2, 1000)
y = np.exp(x)
plt.plot(x, y, ls="-", lw=2, color="g")
plt.title("center demo")
plt.title("left demo", loc="left",
fontdict={"size": "xx-large",
"color": "r",
"family": "Times New Roman"})
plt.title("right demo", loc="right",
family="Comic Sans MS", size=20,
style="oblique", color="c")
plt.show()
主要參數都在上面代碼里體現了

1.11 legend()——表示不同圖形的文本標簽圖例
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
x = np.arange(0, 2.1, 0.1)
y = np.power(x, 3)
y1 = np.power(x, 2)
y2 = np.power(x, 1)
plt.plot(x, y, ls="-", lw=2, label="$x^3$")
plt.plot(x, y1, ls="-", lw=2, label="$x^2$")
plt.plot(x, y2, ls="-", lw=2, label="$x^1$")
plt.legend(loc="upper left",fontsize="x-large", bbox_to_anchor=(0.05, 0.95), ncol=3,
title="power function", shadow=True, fancybox=True)
plt.show()
loc
參數控制圖例的位置,可選值為:
best
upper right
upper left
lower left
lower right
right
center left
center right
lower center
upper center
center
fontsize
控制圖例字體大小,可選值為:
int
float
xx-small
x-small
small
medium
large
x-large
xx-large
frameon
:True
或False
,是否顯示圖例邊框edgecolor
:圖例邊框顏色facecolor
:圖例背景顏色,若無邊框,參數無效title
:設置圖例標題fancybox
:True
表示線框直角,False
表示線框圓角shadow
:True
或False
,是否顯示陰影

2. 常用配置參數
2.1 線型
linestyle
或ls
-
:實線--
:虛線-.
:點划線:
:點線
2.2 線寬
linewidth
或lw
- 浮點數
2.3 線條顏色
color
或c
b
:blue,藍色g
:green,綠色r
:red,紅色c
:cyan,藍綠m
:magenta,洋紅y
:yellow,黃色k
:black,黑色w
:white,白色
也可以對關鍵字參數color賦十六進制的RGB字符串如 color='#900302'
2.4 點標記類型
marker
,只能用以下簡寫符號表示
.
:point marker,
:pixel markero
:circle markerv
:triangle_down marker^
:triangle_up marker<
:triangle_left marker>
:triangle_right marker1
:tri_down marker2
:tri_up marker3
:tri_left marker4
:tri_right markers
:square markerp
:pentagon marker*
:star markerh
:hexagon1 markerH
:hexagon2 marker+
:plus markerx
:x markerD
:diamond markerd
:thin_diamond marker|
:vline marker_
:hline marker
特別地,標記還有mathtext模式
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.use('Qt5Agg')
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['font.serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False # 解決保存圖像是負號'-'顯示為方塊的問題,或者轉換負號為字符串
x = np.arange(1, 13, 1)
y = np.array([12, 34, 22, 30, 18, 13, 15, 19, 24, 28, 23, 27])
fig, ax = plt.subplots(2, 2)
ax[0, 0].scatter(x, y * 1.5, marker=r"$\clubsuit$", c="#fb8072", s=500)
ax[0, 0].locator_params(axis="x", tight=True, nbins=11)
ax[0, 0].set_xlim(0, 13)
ax[0, 0].set_xticks(x)
ax[0, 0].set_title('顯示樣式{}的散點圖'.format(r"$\clubsuit$"))
ax[0, 1].scatter(x, y - 2, marker=r"$\heartsuit$", c="#fb8072", s=500)
ax[0, 1].locator_params(axis="x", tight=True, nbins=11)
ax[0, 1].set_xlim(0, 13)
ax[0, 1].set_xticks(x)
ax[0, 1].set_title('顯示樣式{}的散點圖'.format(r"$\heartsuit$"))
ax[1, 0].scatter(x, y + 7, marker=r"$\diamondsuit$", c="#fb8072", s=500)
ax[1, 0].locator_params(axis="x", tight=True, nbins=11)
ax[1, 0].set_xlim(0, 13)
ax[1, 0].set_xticks(x)
ax[1, 0].set_title('顯示樣式{}的散點圖'.format(r"$\diamondsuit$"))
ax[1, 1].scatter(x, y - 9, marker=r"$\spadesuit$", c="#fb8072", s=500)
ax[1, 1].locator_params(axis="x", tight=True, nbins=11)
ax[1, 1].set_xlim(0, 13)
ax[1, 1].set_xticks(x)
ax[1, 1].set_title('顯示樣式{}的散點圖'.format(r"$\spadesuit"))
plt.suptitle("不同原始字符串作為標記類型的展示效果", fontsize=16, weight="black")
plt.show()

官網有一張屬性表,先貼在這,以后有空會再補充內容的
