python__matplotlib添加公式,标记,子图结构

先看结果,有你能用到的再去找程序,几乎每行程序都有注释。
在手机上看可能会导致程序显示不完整

添加标记

import matplotlib.pyplot as plt
plt.figure(1,figsize=(5,5))
ax = plt.subplot(111)#
plt.xticks(range(6))
plt.yticks(range(6))#如果不指定刻度,x轴与y轴都是1
ann = ax.annotate(u"箭头",xy=(1,1), #注解
                    xytext=(4,4),size=20,
                    va="center",ha="center",
                    bbox=dict(boxstyle='sawtooth',fc="w"),
                    arrowprops=dict(arrowstyle="-|>",#"-|>"代表箭头头上是实心的
                                    connectionstyle="angle,rad=0.4",fc='r')#rad代表箭头是否是弯的,+-定义弯的方向
                    )
#xy箭头的位置
#xytext文本框的位置
#size文本框的大小
#va,ha字体显示在文本框的位置
#文本框边框bbox=dict(boxstyle=边框样式,fc=前景色)
#箭头arrowprops=dict(arrowsyle=箭头样式,connectionstyle=连接路径arc3直接连,完全角度)
ax.grid(True)
plt.show()

结果

《python__matplotlib添加公式,标记,子图结构》 添加标记

添加公式

import matplotlib.pyplot as plt
fig = plt.figure() #figsize=(10,6)
ax= fig.add_subplot(111)
ax.set_xlim([1, 6]);
ax.set_ylim([1, 9]);
ax.text(2, 8,  r"$ \mu \alpha \tau \pi \lambda \omega \tau \
    lambda \iota \beta $",color='r',fontsize=20);
ax.text(2, 6, r"$ \lim_{x \rightarrow 0} \frac{1}{x} $",fontsize=20);
ax.text(2, 4, r"$ a \ \leq \ b \ \leq \ c \ \Rightarrow \ a \
    \leq \ c$",fontsize=20);
ax.text(2, 2, r"$ \sum_{i=1}^{\infty}\ x_i^2$",fontsize=20);
ax.text(4, 8, r"$ \sin(0) = \cos(\frac{\pi}{2})$",fontsize=20);
ax.text(4, 6, r"$ \sqrt[3]{x} = \sqrt{y}$",fontsize=20);
ax.text(4, 4, r"$ \neg (a \wedge b) \Leftrightarrow \neg a \
    \vee \neg b$");
ax.text(4, 2, r"$ \int_a^b f(x)dx$",fontsize=20);
plt.show()

结果:

《python__matplotlib添加公式,标记,子图结构》 添加公式

添加子图结构

import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0,5,0.2)#创建一个数组.arange相当于range,0到5,以0.2作为间隔
fig = plt.figure()#figsize=(10,6)

#行, 列, 序号
ax1 = fig.add_subplot(221) #表示在2*2的网格的格式里,占第一个位置
ax2 = fig.add_subplot(222) #表示在2*2的网格的格式里,占第二个位置
ax3 = fig.add_subplot(212) #表示在2*1的网格的格式里,占第2个位置
#所有图加起来的总大小是定了的
#第一个子图里画3条线t,t^2,t^3
ax1.plot(t,t,"r--",t,t**2,'b',t,t**3,'g^')
ax1.set_title(u'图一plot')#不是子图时,方法是.title,子图时方法是.set_title
ax2.semilogy(t,t,"r--",t,t**2,'b',t,t**3,'g^')
ax2.set_title(u"图二semilogy")
#把y轴取对数
ax3.loglog(t,t,"r--",t,t**2,'b',t,t**3,'g^')
ax3.set_title(u"图三loglog")
#把x,y都取log
#大图的标题
fig.suptitle('subplot training')
#增加子图间的间隔
fig.subplots_adjust(hspace=0.4)
plt.show()

结果

《python__matplotlib添加公式,标记,子图结构》 子图结构

    原文作者:Kedi
    原文地址: https://www.jianshu.com/p/3edb07f58ecd
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞