http://blog.csdn.net/pipisorry/article/details/37766161
遠(yuǎn)程繪圖,在matplotlib中的一個坐標(biāo)軸上畫一條直線光標(biāo),繪制LaTeX數(shù)學(xué)公式,對數(shù)坐標(biāo)軸。
python使用ssh進(jìn)行遠(yuǎn)程解釋器繪圖時出錯:RuntimeError: Invalid DISPLAY variable
主要原因:
By default, matplotlib will use something like the TkAgg backend. This requires an X-server to be running.
解決1:
import matplotlibmatplotlib.use('Agg') # Must be before importing matplotlib.pyplot or pylab!import matplotlib.pyplot as plt
[How to save a figure remotely with pylab? [duplicate]]
解決2:
[How do you plot a graph when developing on a remote machine through ssh only]
[Matplotlib: display plot on a remote machine]
解決3:代碼在遠(yuǎn)程執(zhí)行,而在本地繪圖
There are a few possibilities
If your remote machine is somehow unixish, you may use the X Windows (then your session is on the remote machine and display on the local machine)
mpld3
bokeh and iPython notebook
nbagg backend of matplotlib.¨
Alternative #1 requires you to have an X server on your machine and a connection between the two machines (possibly tunneled through ssh, etc.) So, this is OS dependent, and the performance depends on the connection between the two machines.
Alternatives #2 and #3 are very new but promising. They have quite different approaches, mpl3d enables the use of standard matplotlib plotting commands, but with large datasets bokeh may be more useful.
Alternative #4 is probably the ultimate solution (see tcaswell's comments), but not yet available without using a development version of matplotlib (i.e. there may be some installation challenges). On the other hand, if you can hold your breath for a week, 1.4.0 will be out.
The upcoming release (1.4.0, should be out by end of August 2014, release candidates are available) will ship with the nbagg backend which provides interactive figures with out needing to go to native clients or resorting to using d3. All you need to do in your note book is:
import matplotlibmatplotlib.use('nbagg')from matplotlib import pyplot as plt
And then to plot
plt.plot(range(3))plt.show()
[how to display matplotlib plots on local machine?]
1) on remote host (VPS, Ubuntu 16.04) I had to install X11 server, which I did by:
sudo apt-get install xorgsudo apt-get install openbox
2) On remote host I had to make sure that X11Forwarding is enabled in /etc/ssh/sshd_config
3) On local Win10 machine I had to install Xming server and launch it with default settings.
4) On local Win10 machine I had to configure Putty to use X11 forwarding (Connection-> SSH -> X11 Forwarding) with default settings and keep connection open while running PyCharm (it seems there is no option in PyCharm to enable x11 forwarding, so putty must be running in the background)
5) On remote machine I had to check Display number (echo $DISPLAY) - this can be different for everyone. For me it was localhost:10.0
6) In PyCharm Run configuration -> Environment variables I had to add DISPLAY=localhost:10.0
After all these steps and Putty+Xming running in backgroud, I was able to execute remote code and bring graphic back to my Windows 10 PC!
[Python plotting on remote server using PyCharm]# set useblit = True on gtkagg for enhanced performance
# horizOn=True時,兩個坐標(biāo)都有顯示光標(biāo)
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.widgets import Cursort = np.arange(0.0, 2.0, 0.01)s1 = np.sin(2 * np.pi * t)plt.plot(t, s1)cursor = Cursor(plt.gca(), horizOn=True, color='r', lw=1)plt.show()
Note: 一個神奇的事情就是,Cursor()必須有一個賦值給cursor,否則并不會顯示光標(biāo)(ubuntu16.04下)。之前在windows下繪制時不用賦值也是會有光標(biāo)顯示的。
結(jié)果示圖(隨著光標(biāo)的移動,在圖中x坐標(biāo)上會畫一條豎線,并在下方顯示坐標(biāo)):
同時在兩個子圖的兩個坐標(biāo)軸之間畫一條直線光標(biāo)
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.widgets import MultiCursort = np.arange(0.0, 2.0, 0.01)s1 = np.sin(2*np.pi*t)s2 = np.sin(4*np.pi*t)fig = plt.figure()ax1 = fig.add_subplot(211)ax1.plot(t, s1)ax2 = fig.add_subplot(212, sharex=ax1)ax2.plot(t, s2)multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1)plt.show()
[widgets example code: multicursor.py]
1. matplotlib.rcParams屬性字典,想要它正常工作,在matplotlibrc配置文件中需要設(shè)置text.markup = "tex"。
2. 如果你希望圖表中所有的文字(包括坐標(biāo)軸刻度標(biāo)記)都是LaTeX'd,需要在matplotlibrc中設(shè)置text.usetex = True。如果你使用LaTeX撰寫論文,那么這一點(diǎn)對于使圖表和論文中其余部分保持一致是很有用的。
Matlplotlib對LaTeX有一定的支持,如果記得使用raw字符串語法會很自然:xlabel(r"")You can use a subset TeX markup in any matplotlib text string byplacing it inside a pair of dollar signs ($).mathtext開始和結(jié)束的字符都要是 $ 。
Note that you do not need to have TeX installed, since matplotlibships its own TeX expression parser, layout engine and fonts.
在matplotlib里面,可以使用LaTex的命令來編輯公式,只需要在字符串前面加一個“r”即可。(在python raw string需要r‘’,表示不轉(zhuǎn)義)Any text element can use math text. You should use raw strings(precede the quotes with an 'r'
), and surround the math text withdollar signs ($)
Here is a simple example: plt.title('alpha > beta')
produces “alpha > beta”.
Whereas this:
plt.title(r'$\alpha > \beta$')
produces "
To make subscripts and superscripts, use the '_'
and '^'
symbols:
r'$\alpha_i > \beta_i$'
符號表示及參考[Writing mathematical expressions]
1
import numpy as npimport matplotlib.pyplot as pltt = np.arange(0.0, 2.0, 0.01)s = np.sin(2*np.pi*t)plt.plot(t,s)plt.title(r'$\alpha_i > \beta_i$', fontsize=20)plt.text(1, -0.6, r'$\sum_{i=0}^\infty x_i$', fontsize=20)plt.text(0.6, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$', fontsize=20)plt.xlabel('time (s)')plt.ylabel('volts (mV)')plt.show()
2
In [6]: ax.text(2, 8, r"$ \mu \alpha \tau \pi \lambda \omega \tau \lambda \iota \beta $");
In [7]: ax.text(2, 6, r"$ \lim_{x \rightarrow 0} \frac{1}{x} $");
In [8]: ax.text(2, 4, r"$ a \ \leq \ b \ \leq \ c \ \Rightarrow \ a \ \leq \ c$");
In [9]: ax.text(2, 2, r"$ \sum_{i=1}^{\infty}\ x_i^2$");
In [10]: ax.text(4, 8, r"$ \sin(0) = \cos(\frac{\pi}{2})$");
In [11]: ax.text(4, 6, r"$ \sqrt[3]{x} = \sqrt{y}$");
In [12]: ax.text(4, 4, r"$ \neg (a \wedge b) \Leftrightarrow \neg a \vee \neg b$");
In [13]: ax.text(4, 2, r"$ \int_a^b f(x)dx$");
[Matplotlib for Python Developers]
在實(shí)際中,我們可能經(jīng)常會用到對數(shù)坐標(biāo)軸,這時可以用下面的三個函數(shù)來實(shí)現(xiàn)
ax.semilogx(x,y) #x軸為對數(shù)坐標(biāo)軸
ax.semilogy(x,y) #y軸為對數(shù)坐標(biāo)軸
ax.loglog(x,y) #雙對數(shù)坐標(biāo)軸
from:http://blog.csdn.net/pipisorry/article/details/37766161
ref: