這次一定。。。一定總結一波最全的python畫圖技巧。。。
首先用的是 matplotlib 和 pyplot。
import
numpy
as
np
import
matplotlib
as
mpl
import
matplotlib。pyplot
as
plt
生成一組資料
x
=
np
。
linspace
(
0
,
5
,
10
)
y
=
x
**
2
最基本的畫圖:
plt
。
figure
()
plt
。
plot
(
x
,
y
,
‘r’
)
plt
。
xlabel
(
‘x’
)
plt
。
ylabel
(
‘y’
)
plt
。
title
(
‘title’
)
plt
。
show
()
結果:
在一個圖裡繪製多組資料
x
=
np
。
linspace
(
0
,
5
,
10
)
y
=
[
x
**
t
for
t
in
range
(
1
,
4
)]
plt
。
figure
()
for
item
in
y
:
plt
。
plot
(
x
,
item
,
‘r’
)
plt
。
xlabel
(
‘x’
)
plt
。
ylabel
(
‘y’
)
plt
。
title
(
‘title’
)
plt
。
show
()
基本元素
這張圖很好的表示了一張圖擁有的各種
屬性
。
美化之路從現在開始~
美化線型
plt
。
figure
()
plt
。
plot
(
x
,
y
[
0
],
‘g*-’
)
plt
。
xlabel
(
‘x’
)
plt
。
ylabel
(
‘y’
)
plt
。
title
(
‘title’
)
plt
。
show
()
紅實線變成了綠色星線。
顏色
藍色: ‘b’ (blue)
綠色: ‘g’ (green)
紅色: ‘r’ (red)
藍綠色(墨綠色): ‘c’ (cyan)
紅紫色(洋紅): ‘m’ (magenta)
黃色: ‘y’ (yellow)
黑色: ‘k’ (black)
白色: ‘w’ (white)
灰度表示: e。g。 0。75 ([0,1]內任意浮點數)
RGB表示法: e。g。 ‘#2F4F4F’ 或 (0。18, 0。31, 0。31)
任意合法的html中的顏色表示: e。g。 ‘red’, ‘darkslategray’
線型
實線: ‘-’
虛線: ‘——’
虛點線: ‘-。’
點線: ‘:’
點: ‘。’
點型(marker)
畫素: ‘,’
圓形: ‘o’
上三角: ‘^’
下三角: ‘v’
左三角: ‘<’
右三角: ‘>’
方形: ‘s’
加號: ‘+’
叉形: ‘x’
稜形: ‘D’
細稜形: ‘d’
三腳架朝下: ‘1’(就是丫)
三腳架朝上: ‘2’
三腳架朝左: ‘3’
三腳架朝右: ‘4’
六角形: ‘h’
旋轉六角形: ‘H’
五角形: ‘p’
垂直線: ‘|’
水平線: ‘_’
gnuplot 中的steps: ‘steps’ (只能用於kwarg中)
其他屬性:
標記大小(markersize 簡寫為 ms): markersize: 實數
標記邊緣寬度(markeredgewidth 簡寫為 mew):markeredgewidth:實數
標記邊緣顏色(markeredgecolor 簡寫為 mec):markeredgecolor:顏色選項中的任意值
標記表面顏色(markerfacecolor 簡寫為 mfc):markerfacecolor:顏色選項中的任意值
透明度(alpha):alpha: [0,1]之間的浮點數
線寬(linewidth):linewidth: 實數
看看效果:
plt
。
plot
(
x
,
y
[
0
],
‘m——’
,
marker
=
‘D’
,
linewidth
=
2。5
,
alpha
=
0。8
)
具體好看合適用在論文的組合我還沒總結過,這裡挖個坑:
完美線型組合
如何畫子圖
生成多組資料:
x
=
np
。
linspace
(
0
,
5
,
10
)
y
=
[
np
。
log
(
x
)
/
np
。
log
(
t
)
for
t
in
range
(
2
,
6
)]
畫到子圖中去:
n_row
=
2
n_col
=
2
fig
,
axs
=
plt
。
subplots
(
n_row
,
n_col
,
figsize
=
(
9
,
9
))
line_sst
=
[
‘r*——’
,
‘g*-’
,
‘m——’
,
‘grey’
]
marker_sst
=
[
‘o’
,
‘+’
,
‘x’
,
‘D’
]
idx
=
0
for
row
in
range
(
n_row
):
for
col
in
range
(
n_col
):
axs
[
row
,
col
]
。
plot
(
y
[
idx
],
line_sst
[
idx
],
marker
=
marker_sst
[
idx
],
linewidth
=
1。5
,
label
=
‘log{}’
。
format
(
idx
+
2
))
axs
[
row
,
col
]
。
legend
(
loc
=
0
)
idx
+=
1
plt
。
show
()
增加x,y,title
n_row
=
2
n_col
=
2
fig
,
axs
=
plt
。
subplots
(
n_row
,
n_col
,
figsize
=
(
9
,
9
))
line_sst
=
[
‘r*——’
,
‘g*-’
,
‘m——’
,
‘grey’
]
marker_sst
=
[
‘o’
,
‘+’
,
‘x’
,
‘D’
]
label_sst
=
[]
x_lab_sst
=
[
‘x’
,
‘x’
,
‘x’
,
‘x’
]
y_lab_sst
=
[
‘y’
,
‘y’
,
‘y’
,
‘y’
]
title_sst
=
[
‘title_1’
,
‘title_2’
,
‘title_3’
,
‘title_4’
]
idx
=
0
for
row
in
range
(
n_row
):
for
col
in
range
(
n_col
):
ax
=
axs
[
row
,
col
]
ax
。
plot
(
y
[
idx
],
line_sst
[
idx
],
marker
=
marker_sst
[
idx
],
linewidth
=
1。5
,
label
=
‘log{}’
。
format
(
idx
+
2
))
ax
。
legend
(
loc
=
0
)
# 圖例位置
ax
。
set_xlabel
(
x_lab_sst
[
idx
])
ax
。
set_ylabel
(
y_lab_sst
[
idx
])
ax
。
set_title
(
title_sst
[
idx
])
idx
+=
1
plt
。
tight_layout
()
# 讓圖形不那麼擠
plt
。
show
()
更改字型
n_row
=
2
n_col
=
2
fig
,
axs
=
plt
。
subplots
(
n_row
,
n_col
,
figsize
=
(
9
,
9
))
line_sst
=
[
‘r*——’
,
‘g*-’
,
‘m——’
,
‘grey’
]
marker_sst
=
[
‘o’
,
‘+’
,
‘x’
,
‘D’
]
label_sst
=
[]
x_lab_sst
=
[
‘x’
,
‘x’
,
‘x’
,
‘x’
]
y_lab_sst
=
[
‘y’
,
‘y’
,
‘y’
,
‘y’
]
title_sst
=
[
‘title_1’
,
‘title_2’
,
‘title_3’
,
‘title_4’
]
font
=
{
“color”
:
“darkred”
,
“size”
:
14
,
“family”
:
“times new roman”
}
idx
=
0
for
row
in
range
(
n_row
):
for
col
in
range
(
n_col
):
ax
=
axs
[
row
,
col
]
ax
。
plot
(
y
[
idx
],
line_sst
[
idx
],
marker
=
marker_sst
[
idx
],
linewidth
=
1。5
,
label
=
‘log{}’
。
format
(
idx
+
2
))
ax
。
legend
(
loc
=
0
)
ax
。
set_xlabel
(
x_lab_sst
[
idx
],
fontdict
=
font
)
ax
。
set_ylabel
(
y_lab_sst
[
idx
],
fontdict
=
font
)
ax
。
set_title
(
title_sst
[
idx
],
fontdict
=
font
)
for
label
in
ax
。
get_xticklabels
()
+
ax
。
get_yticklabels
():
label
。
set_fontname
(
‘Times New Roman’
)
idx
+=
1
plt
。
tick_params
(
labelsize
=
12
)
plt
。
tight_layout
()
plt
。
show
()
圖例位置:
‘best’,‘upper right’, ‘upper left’, ‘lower left’, ‘lower right’, ‘right’, ‘center left’, ‘center right’, ‘lower center’, ‘upper center’, ‘center’
也可以直接用對應索引,比如best對應的是0。
也可以手動規範圖例,需要傳入控制代碼:
plt
。
legend
(
handles
=
[
ax1
,
ax2
,],
labels
=
[
‘a’
,
‘b’
],
loc
=
0
)
在文本里輸入公式
title_sst
=
[
r
‘$\alpha$’
,
r
‘$log_3$’
,
r
‘$\sum{x^2}$’
,
r
‘$\hat{x}$’
]
雙座標系
被抓走乾貨,得空再更
參考
Python中畫圖時候的線型別_lei_jw的部落格-CSDN部落格_python 畫線的種類
2。 matplotlib-legend 位置屬性 loc 使用
3。【原】在Matplotlib繪圖中新增Latex風格公式 - ChaoSimple - 部落格園
4。
https://www。
southampton。ac。uk/~feeg
1001/notebooks/Matplotlib。html
To be continue