Plot in Qt Window
To show images within Python console, you need install a backend first:
pip install PyQt5
.
Then start IPython:
$ cat << EOF > testImg.py
import numpy as np
import matplotlib.pyplot as plt
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)
plt.plot(X, y, "b.") # image window appear at this step
# the image is updated with the following 3 commands
# (therefor `plt.show()` is unnecessary)
plt.xlabel("$x_1$", fontsize=18)
plt.ylabel("$y$", rotation=0, fontsize=18)
plt.axis([0, 2, 0, 15])
EOF
$ ipython
>>> import matplotlib.pyplot as plt
>>> plt.get_backend() # verify current backend
>>> %load testImg.py
You can run above codes and show Qt window from IPython (or bpython, ptpython, but not ptipython, don't know why) console within a Tmux session.
Verified on Linux Mint 19.3, 2020-5-24 8:38.
Note:
ipython --matplotlib=qt
is unnecessary after PyQt5 is installed.
Ref:
Search "Qt5Agg" in Usage Guide of matplotlib.
Plot in Browser
With plotly, you can show plots in browser:
$ pip install plotly
$ cat << EOF > demo.py
import plotly.express as px
df = px.data.iris()
px.scatter(df, x="sepal_width", y="sepal_length").show()
EOF
$ python demo.py
Plot on Remote Server
Say you ssh to ServerA to run a Python REPL from laptopB (Linux os).
First you need add X11Forwording option when SSH to host with
ssh -X ServerA
for one-shot,
or add it to the ssh configuration (~/.ssh/config) for laptopB:
Host tc
Hostname 192.168.17.253
User leo
ForwardX11 yes
For Windows client, the X11 forwarding support of WSL2 doesn't work (2021.1.11), you can use MobaXterm as the client. It opens X11 Forwording by default when define a new SSH coonection.
However, the keyboard shortcut for tmux (Alt-1, Alt-2) doesn't work in MobaXterm. So when X11 Forwarding is not a must-have, WSL is a better choice.
matplotlib 架构
matplotlib 的设计原理和发展历史可参考 "Mastering matplotlib-Duncan M. McGreggor(2015).pdf" 第2章 "The matplotlib Architecture",以及 "Python Data Analytics with Pandas NumPy and Matplotlib-Fabio Nelli(2018).pdf" 第7章 "The IPython and IPython QtConsole" 和 "The matplotlib Architecture" 两节, 厘清了 matplot 中最重要的几个概念的联系和区别:
-
"pyplot" 是 matplotlib API 模块 (scripting layer) 的名称;
-
"figure" 代表整个绘图区域,可能只包含一个图,也可能包含多个图(subplot);
-
"axes" 在 matplotlib 中代表一个图,也就是 subplot 中的一个小图, 其类名是 AxesSubplot,在单图场景中,"figure" 和 "axes" 基本是重合的;
-
"axis" 代表一个坐标轴,二维图中有x轴和y轴两种情况;
参考下面的代码:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
f, axarr = plt.subplots(2, 2)
print(type(plt)) # <class 'module'>
print(type(f)) # <class 'matplotlib.figure.Figure'>
print(type(axarr)) # <class 'numpy.ndarray'>
axarr[0, 0].plot(x, y)
axarr[0, 0].set_title('Axis [0,0]')
axarr[0, 1].scatter(x, y)
axarr[0, 1].set_title('Axis [0,1]')
axarr[1, 0].plot(x, y ** 2)
axarr[1, 0].set_title('Axis [1,0]')
axarr[1, 1].scatter(x, y ** 2)
axarr[1, 1].set_title('Axis [1,1]')
print(type(axarr[0, 0])) # <class 'matplotlib.axes._subplots.AxesSubplot'>
print(type(axarr[0,0].xaxis)) # <class 'matplotlib.axis.XAxis'>
plt.show()
Note:
axes 本意是 axis(轴)或者 ax(斧子)的复数,具体哪个意思取决于上下文。
在单图场景中,figure 对象通过 fig = plt.figure()
方法获得。