Files
notes_estom/Python/matplotlab/gallery/misc/pythonic_matplotlib.md
2020-09-26 22:03:11 +08:00

72 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Pythonic Matplotlib
有些人喜欢编写更多的面向对象的Python代码而不是使用pyPLOT接口来编写matplotlib。此示例向您展示了如何实现。
除非您是应用程序开发人员否则我建议使用部分pyplot接口尤其是图形closesubplotaxes和show命令。 这些隐藏了您在正常图形创建中不需要看到的很多复杂性例如实例化DPI实例管理图形元素的边界框创建和实现GUI窗口以及在其中嵌入图形。
如果您是应用程序开发人员并希望在应用程序中嵌入matplotlib请遵循示例/ embedding_in_wx.pyexamples / embedding_in_gtk.py或examples / embedding_in_tk.py的主题。 在这种情况下,您需要控制所有图形的创建,将它们嵌入应用程序窗口等。
如果您是Web应用程序开发人员您可能希望使用webapp_demo.py中的示例该示例显示如何直接使用后端agg图形画布而不包含pyplot中存在的全局变量当前图形当前轴 接口。 但请注意没有理由说pyplot接口不适用于Web应用程序开发人员。
如果您在pyplot接口中编写的示例目录中看到一个示例并且您希望使用真正的python方法调用来模拟它则可以轻松进行映射。 其中许多示例使用“set”来控制图形属性。 以下是将这些命令映射到实例方法的方法。
set的语法是
```python
plt.setp(object or sequence, somestring, attribute)
```
如果使用对象调用,则设置调用:
```python
object.set_somestring(attribute)
```
如果使用序列调用则set
```python
for object in sequence:
object.set_somestring(attribute)
```
因此对于您的示例如果a是您的轴对象则可以执行以下操作
```python
a.set_xticklabels([])
a.set_yticklabels([])
a.set_xticks([])
a.set_yticks([])
```
![Pythonic Matplotlib示例](https://matplotlib.org/_images/sphx_glr_pythonic_matplotlib_001.png)
```python
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0.0, 1.0, 0.01)
fig, (ax1, ax2) = plt.subplots(2)
ax1.plot(t, np.sin(2*np.pi * t))
ax1.grid(True)
ax1.set_ylim((-2, 2))
ax1.set_ylabel('1 Hz')
ax1.set_title('A sine wave or two')
ax1.xaxis.set_tick_params(labelcolor='r')
ax2.plot(t, np.sin(2 * 2*np.pi * t))
ax2.grid(True)
ax2.set_ylim((-2, 2))
l = ax2.set_xlabel('Hi mom')
l.set_color('g')
l.set_fontsize('large')
plt.show()
```
## 下载这个示例
- [下载python源码: pythonic_matplotlib.py](https://matplotlib.org/_downloads/pythonic_matplotlib.py)
- [下载Jupyter notebook: pythonic_matplotlib.ipynb](https://matplotlib.org/_downloads/pythonic_matplotlib.ipynb)