matplotlib绘图进阶

http://blog.csdn.net/pipisorry/article/details/37766161html

远程绘图,在matplotlib中的一个坐标轴上画一条直线光标,绘制LaTeX数学公式,对数坐标轴。
python

远程绘图

python使用ssh进行远程解释器绘图时出错:RuntimeError: Invalid DISPLAY variableexpress

主要缘由:canvas

By default, matplotlib will use something like the TkAgg backend. This requires an X-server to be running.ubuntu

解决1:windows

import matplotlib matplotlib.use('Agg') # Must be before importing matplotlib.pyplot or pylab! import matplotlib.pyplot as plt
可是这个并不能使代码在远程执行,而在本地绘图,只能将绘图保存为图片文件再下载下来看。

[How to save a figure remotely with pylab? [duplicate]]api

解决2:
session

[How do you plot a graph when developing on a remote machine through ssh only]app

[Matplotlib: display plot on a remote machine]
ssh

解决3:代码在远程执行,而在本地绘图

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 matplotlib
matplotlib.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 xorg sudo 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]

皮皮blog





在matplotlib中的一个坐标轴上画一条直线光标

matplotlib.widgets.Cursor

# set useblit = True on gtkagg for enhanced performance

# horizOn=True时,两个坐标都有显示光标

[matplotlib.widgets.Cursor]

示例

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor

t = 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,不然并不会显示光标(ubuntu16.04下)。以前在windows下绘制时不用赋值也是会有光标显示的。

结果示图(随着光标的移动,在图中x坐标上会画一条竖线,并在下方显示坐标):

同时在两个子图的两个坐标轴之间画一条直线光标

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import MultiCursor

t = 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]

皮皮blog




绘制LaTeX数学公式

1. matplotlib.rcParams属性字典,想要它正常工做,在matplotlibrc配置文件中须要设置text.markup = "tex"。

2. 若是你但愿图表中全部的文字(包括坐标轴刻度标记)都是LaTeX'd,须要在matplotlibrc中设置text.usetex = True。若是你使用LaTeX撰写论文,那么这一点对于使图表和论文中其他部分保持一致是颇有用的。

Matlplotlib对LaTeX有必定的支持,若是记得使用raw字符串语法会很天然:xlabel(r" x2y4")

书写数学公式

You can use a subset TeX markup in any matplotlib text string byplacing it inside a pair of dollar signs ($).mathtext开始和结束的字符都要是 $ 。

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‘’,表示不转义)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 "".

Subscripts and superscripts

To make subscripts and superscripts, use the '_' and '^' symbols:

r'$\alpha_i > \beta_i$'
双下标表示 E_{ij}

符号表示及参考[Writing mathematical expressions]

示例

1

import numpy as np
import matplotlib.pyplot as plt
t = 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()
../_images/pyplot_mathtext.png

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$");

image

[Text rendering With LaTeX]

[Matplotlib for Python Developers]

[LaTeX科技排版]

皮皮blog


对数坐标轴

在实际中,咱们可能常常会用到对数坐标轴,这时能够用下面的三个函数来实现

ax.semilogx(x,y) #x轴为对数坐标轴

ax.semilogy(x,y) #y轴为对数坐标轴

ax.loglog(x,y) #双对数坐标轴

from:http://blog.csdn.net/pipisorry/article/details/37766161

ref: