matplotlib学习1

一、向几何图形里填充颜色

1.1 不规则多边形的颜色填充

在这里插入图片描述

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,2*np.pi,500)
y = np.sin(x)

plt.fill(x,y,color="cornflowerblue",alpha=0.4)
plt.plot(x,y,color="cornflowerblue",alpha=0.4)
plt.plot([x[0],x[-1]],[y[0],y[-1]],color="cornflowerblue",alpha=0.8)
plt.xlim(0,2*np.pi)
plt.ylim(-1.1,1.1)
plt.show()

1.2 交叉曲线的颜色填充

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,2,500)
y1 = np.sin(2*np.pi*x)
y2 = 1.1*np.sin(3*np.pi*x)
fig,ax = plt.subplots(3,1,sharex="all") #获得一个画布对象fig,和一个坐标轴列表ax

ax[0].fill_between(x,0,y2,alpha=0.5)
ax[0].set_ylim(-1.2,1.2)

ax[1].fill_between(x,y2,1.1,alpha=0.5)
ax[1].set_ylim(-1.2,1.2)

ax[2].fill_between(x,y1,y2,alpha=0.5)
ax[2].set_xlim(0,2)
ax[2].set_ylim(-1.2,1.2)

plt.show()

1.3 水平方向的交叉曲线的颜色填充

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,2,500)
y1 = np.sin(2*np.pi*x)
y2 = 1.1*np.sin(3*np.pi*x)

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(x,y1,color="k",lw=1,ls="-")
ax.plot(x,y2,color="k",lw=1,ls="-")

ax.fill_between(x,y1,y2,where=y2>y1,interpolate=True,facecolor="cornflowerblue",alpha=0.7)
ax.fill_between(x,y1,y2,where=y1>y2,interpolate=True,facecolor="darkred",alpha=0.7)
ax.set_xlim(0,2)
ax.set_ylim(-1.2,1.2)
ax.grid(ls=":",lw=1,color="gray",alpha=0.5)
plt.show()

1.4 垂直方向的交叉曲线的颜色填充

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

y = np.linspace(0,2,500)
x1 = np.sin(2*np.pi*y)
x2 = 1.1*np.sin(3*np.pi*y)

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(x1,y,color="k",lw=1,ls="-")
ax.plot(x2,y,color="k",lw=1,ls="-")

ax.fill_betweenx(y,x1,x2,where=x2>x1,interpolate=True,facecolor="cornflowerblue",alpha=0.7)
ax.fill_betweenx(y,x1,x2,where=x2<x1,interpolate=True,facecolor="darkred",alpha=0.7)
ax.set_xlim(-1.2,1.2)
ax.set_ylim(0,2)
ax.grid(ls=":",lw=1,color="gray",alpha=0.5)
plt.show()

2.使用patches绘制几何图形

2.1 圆

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle

fig,ax = plt.subplots(2,2)
cirlce = Circle((2,2),radius=2,facecolor="white",edgecolor="cornflowerblue")
ax[0,0].add_patch(cirlce)
ax[0,0].set_xlim(-1,5)
ax[0,0].set_ylim(-1,5)


rectangle = ax[0,1].patch
rectangle.set_facecolor("gold")
cirlce = Circle((2,2),radius=2,facecolor="white",edgecolor="cornflowerblue")
ax[0,1].add_patch(cirlce)
ax[0,1].set_xlim(-1,5)
ax[0,1].set_ylim(-1,5)
ax[0,1].set_aspect("equal","box")

rectangle = ax[1,0].set_facecolor("palegreen")
cirlce = Circle((2,2),radius=2,facecolor="white",edgecolor="cornflowerblue")
ax[1,0].add_patch(cirlce)
ax[1,0].axis("equal")

rectangle = ax[1,1].patch
rectangle.set_facecolor("lightskyblue")
circle = Circle((2,2),radius=2,facecolor="white",edgecolor="cornflowerblue")
ax[1,1].add_patch(circle)
ax[1,1].axis([-1,5,-1,5])
ax[1,1].set_yticks(np.arange(-1,6,1))
ax[1,1].axis("equal")
plt.subplots_adjust(left=0.1)
plt.show()

2.2椭圆

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Ellipse

fig,ax = plt.subplots(1,2,subplot_kw={"aspect":"equal"})

# subplot(121)
angles = np.linspace(0,135,4)

ellipse = [Ellipse((2,2),4,2,a) for a in angles]

for elle in ellipse:
    ax[0].add_patch(elle)
    elle.set_alpha(0.4)
    elle.set_color("cornflowerblue")

ax[0].axis([-1,5,-1,5])

# subplot(122)
num = np.arange(0,100,1)
#np.random.rand(n,m)就是产生[n,m]个0-1的数字
ellipse = [Ellipse(xy=np.random.rand(2)*10,
                   width=np.random.rand(1),
                   height=np.random.rand(1),
                   angle=np.random.rand(1)*360) for i in num]

for elle in ellipse:
    ax[1].add_patch(elle)
    elle.set_alpha(np.random.rand(1))
    elle.set_color(np.random.rand(3)) #生成一个rgb三元数组

ax[1].axis([-1,11,-1,11])

plt.tight_layout()

plt.show()

2.3矩形

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Rectangle

fig,ax = plt.subplots(subplot_kw={"aspect":"equal"})

x1 = np.arange(1,2.6,0.1)
y1 = x1+2

x2 = np.arange(2.5,4.1,0.1)
y2 = -x2+7

# set background color
rectangle = ax.patch
rectangle.set_facecolor("lightskyblue")

# house
rectangle1 = Rectangle((1,0),3,3,facecolor="w",edgecolor="rosybrown")

# door
rectangle2 = Rectangle((1.5,0),1,1.5,facecolor="w",edgecolor="rosybrown",hatch="|||")

# window
rectangle3 = Rectangle((2.9,1.7),0.6,0.6,facecolor="w",edgecolor="rosybrown")

rectangle_list = [rectangle1,rectangle2,rectangle3]

# roof line
ax.plot([1,2.5,4],[3,4.5,3],color="rosybrown")

# window line
ax.plot([3.2,3.2],[1.7,2.3],color="rosybrown")
ax.plot([2.9,3.5],[2.0,2.0],color="rosybrown")

# roof filled color
ax.fill_between(x1,3,y1,color="w",interpolate=True)
ax.fill_between(x2,3,y2,color="w",interpolate=True)

for rect in rectangle_list:
    ax.add_patch(rect)

ax.axis([0,5,0,6])

plt.show()

2.4圆弧的绘制

在这里插入图片描述

2.6 使用折线绘制圆

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle

fig,ax = plt.subplots(2,2)

x = np.linspace(0,2*np.pi,500)
y1= 2*np.cos(x)
y2 = 2*np.sin(x)

# subplot(221)
ax[0,0].plot(y1,y2,color="cornflowerblue",lw=2)

ax[0,0].set_xlim(-3,3)
ax[0,0].set_ylim(-3,3)

# subplot(222)
rectangle = ax[0,1].patch
rectangle.set_facecolor("gold")

ax[0,1].plot(y1,y2,color="cornflowerblue",lw=2)

ax[0,1].set_xlim(-3,3)
ax[0,1].set_ylim(-3,3)

ax[0,1].set_aspect("equal","box")

# subplot(223)
rectangle = ax[1,0].patch
rectangle.set_facecolor("palegreen")

ax[1,0].plot(y1,y2,color="cornflowerblue",lw=2)

ax[1,0].axis("equal")

# subplot(224)
rectangle = ax[1,1].patch
rectangle.set_facecolor("lightskyblue")

ax[1,1].plot(y1,y2,color="cornflowerblue",lw=2)

ax[1,1].axis([-3,3,-3,3])
ax[1,1].set_yticks(np.arange(-3,4,1))

ax[1,1].axis("equal")

plt.subplots_adjust(left=0.1)

plt.show()

2.7 饼图

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Shadow,Wedge

fig,ax = plt.subplots(subplot_kw={"aspect":"equal"})

font_style = {"family":"serif","size":12,"style":"italic","weight":"black"}

sample_data = [350,150,200,300]

total = sum(sample_data)

percents = [i/float(total) for i in sample_data]

angles = [360*i for i in percents]

delta = 45

wedge1 = Wedge((2,2),1,delta,delta+sum(angles[0:1]),color="orange")

wedge2 = Wedge((2,1.9),1,delta+sum(angles[0:1]),delta+sum(angles[0:2]),facecolor="steelblue",edgecolor="white")

wedge3 = Wedge((2,1.9),1,delta+sum(angles[0:2]),delta+sum(angles[0:3]),facecolor="darkred",edgecolor="white")

wedge4 = Wedge((2,1.9),1,delta+sum(angles[0:3]),delta,facecolor="lightgreen",edgecolor="white")

wedges = [wedge1,wedge2,wedge3,wedge4]

for wedge in wedges:
    ax.add_patch(wedge)


ax.text(1.7,2.5,"%3.1f%%" % (percents[0]*100),**font_style)
ax.text(1.2,1.7,"%3.1f%%" % (percents[1]*100),**font_style)
ax.text(1.7,1.2,"%3.1f%%" % (percents[2]*100),**font_style)
ax.text(2.5,1.7,"%3.1f%%" % (percents[3]*100),**font_style)

ax.axis([0,4,0,4])

plt.show()

2.8 圆环图

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Rectangle,Shadow,Wedge

fig,ax = plt.subplots(subplot_kw={"aspect":"equal"})

font_style = {"family":"serif","size":12,"style":"italic","weight":"black"}

sample_data = [350,150,200,300]

total = sum(sample_data)

percents = [i/float(total) for i in sample_data]

angles = [360*i for i in percents]

delta = 45

wedge1 = Wedge((2,1.9),1,delta,delta+sum(angles[0:1]),facecolor="orange",edgecolor="white",width=0.3)

wedge2 = Wedge((2,1.9),1,delta+sum(angles[0:1]),delta+sum(angles[0:2]),facecolor="steelblue",edgecolor="white",width=0.3)

wedge3 = Wedge((2,1.9),1,delta+sum(angles[0:2]),delta+sum(angles[0:3]),facecolor="darkred",edgecolor="white",width=0.3)

wedge4 = Wedge((2,1.9),1,delta+sum(angles[0:3]),delta,facecolor="lightgreen",edgecolor="white",width=0.3)

rectangle = Rectangle((3.0,0.0),1.3,1.3,facecolor="w",edgecolor="rosybrown")

rectangle1 = Rectangle((3.2,0.1),0.3,0.2,color="orange")

rectangle2 = Rectangle((3.2,0.4),0.3,0.2,color="steelblue")

rectangle3 = Rectangle((3.2,0.7),0.3,0.2,color="darkred")

rectangle4 = Rectangle((3.2,1.0),0.3,0.2,color="lightgreen")

wedges = [wedge1,wedge2,wedge3,wedge4,rectangle,rectangle1,rectangle2,rectangle3,rectangle4]

for wedge in wedges:
    ax.add_patch(wedge)

ax.text(3.6,0.1,"%3.1f%%" % (percents[0]*100),**font_style)
ax.text(3.6,0.4,"%3.1f%%" % (percents[1]*100),**font_style)
ax.text(3.6,0.7,"%3.1f%%" % (percents[2]*100),**font_style)
ax.text(3.6,1.0,"%3.1f%%" % (percents[3]*100),**font_style)

ax.axis([0,4.5,-0.5,4])

plt.show()

3. 组合展示统计图形

3.1 判别分析示意图

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

fig,ax = plt.subplots()

num = 50

# new sample
sample = 10*np.random.rand(num,2)
var1 = sample[:,0]
var2 = sample[:,1]

# threshold value
td = 12

# discriminant function
df = 2*var1+var2

cates11 = np.ma.masked_where(df>=td,var1)
cates12 = np.ma.masked_where(df>=td,var2)

cates21 = np.ma.masked_where(df<=td,var1)
cates22 = np.ma.masked_where(df<=td,var2)

ax.scatter(var1,var2,s=cates11*50,marker="s",c=cates11)
ax.scatter(var1,var2,s=cates21*50,marker="o",c=cates21)

ax.plot(var1,-2*var1+12,lw=1,color="b",alpha=0.65)

ax.axis([-1,11,-1,11])

plt.show()

3.2 日期型时间序列图

在这里插入图片描述

import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np

fig,ax = plt.subplots()

months = mdates.MonthLocator() # a Locator instance
dateFmt = mdates.DateFormatter("%m/%d/%y")  # a Formatter instance

# format the ticks
ax.xaxis.set_major_formatter(dateFmt)
ax.xaxis.set_minor_locator(months)
# set appearance parameters for ticks,ticklabels,and gridlines
ax.tick_params(axis="both",direction="out",labelsize=10)

date1 = datetime.date(2008, 4, 17)
date2 = datetime.date(2017, 5, 4)
delta = datetime.timedelta(days=5)
dates = mdates.drange(date1, date2, delta)

y = np.random.normal(100,15,len(dates))

ax.plot_date(dates,y,"b-",alpha=0.7)

fig.autofmt_xdate()

plt.show()

3.3 向直方图中添加概率密度曲线

在这里插入图片描述

# -*- coding:utf-8 -*-

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
#设置中文字体类型是仿宋
mpl.rcParams["font.sans-serif"]=["FangSong"]
mpl.rcParams["axes.unicode_minus"]=False

mu = 60.0
sigma = 2.0
x = mu+sigma*np.random.randn(500)

bins = 50

fig,ax = plt.subplots(1,1)

n,bins,patches = ax.hist(x,
                         bins,
                         normed=True,
                         histtype="bar",
                         facecolor="cornflowerblue",
                         edgecolor="white",
                         alpha=0.75)

y = ((1/(np.power(2*np.pi,0.5)*sigma))*
     np.exp(-0.5*np.power((bins-mu)/sigma,2)))

ax.plot(bins,y,color="orange",ls="--",lw=2)

ax.grid(ls=":",lw=1,color="gray",alpha=0.2)

ax.text(54,0.2,
        r"$y=\frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{(x-\mu)^2}{2\sigma^2}}$",
        {"color": "r", "fontsize": 20})

ax.set_xlabel("体重")
ax.set_ylabel("概率密度")
ax.set_title(r"体重的直方图: $\mu=60.0$, $\sigma=2.0$",fontsize=16)

plt.show()

3.4 向直方图中添加概率密度曲线和积分区域

在这里插入图片描述

# -*- coding:utf-8 -*-

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

from matplotlib.patches import Polygon

mpl.rcParams["font.sans-serif"]=["FangSong"]
mpl.rcParams["axes.unicode_minus"]=False

mu = 60.0
sigma = 2.0
x = mu+sigma*np.random.randn(500)

bins = 50

fig,ax = plt.subplots(1,1)

n,bins,patches = ax.hist(x,
                         bins,
                         normed=True,
                         histtype="bar",
                         facecolor="cornflowerblue",
                         edgecolor="white",
                         alpha=0.75)

y = ((1/(np.power(2*np.pi,0.5)*sigma))*
     np.exp(-0.5*np.power((bins-mu)/sigma,2)))

ax.plot(bins,y,color="orange",ls="--",lw=2)

integ_x = np.linspace(mu-2*sigma,mu+2*sigma,1000)
integ_y = ((1/(np.power(2*np.pi,0.5)*sigma))*
     np.exp(-0.5*np.power((integ_x-mu)/sigma,2)))
area = [(mu-2*sigma,0),*zip(integ_x,integ_y),(mu+2*sigma,0)]

poly = Polygon(area,facecolor="gray",edgecolor="k",alpha=0.6,closed=False)
ax.add_patch(poly)

plt.text(0.45,0.2,
         r"$\int_{\mu-2\sigma}^{\mu+2\sigma} y\mathrm{d}x$",
         fontsize=20,
         transform=ax.transAxes)

ax.grid(ls=":",lw=1,color="gray",alpha=0.2)

ax.text(54,0.2,
        r"$y=\frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{(x-\mu)^2}{2\sigma^2}}$",
        {"color": "r", "fontsize": 20})

ax.set_xlabel("体重")
ax.set_ylabel("概率密度")
ax.set_title(r"体重的直方图: $\mu=60.0$, $\sigma=2.0$",fontsize=16)

plt.show()

3.5 绘图区域嵌套子绘图区域

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

mu = 75.0
sigma = 15.0
bins = 20
x = np.linspace(1,100,200)
y = np.random.normal(mu,sigma,200)

fig,ax = plt.subplots()

# the main axes
ax.plot(x,y,ls="-",lw=2,color="steelblue")
ax.set_ylim(10,170)

# this is an inset axes over the main axes
plt.axes([0.2,0.6,0.2,0.2],fc="k")
count,bins,patches = plt.hist(y,bins,color="cornflowerblue")
plt.ylim(0,28)
plt.xticks([])
plt.yticks([])

# this is an inset axes over the inset axes
plt.axes([0.21,0.72,0.05,0.05])
y1 = (1/(sigma * np.sqrt(2 * np.pi))) * np.exp( - (bins - mu)**2 / (2 * sigma**2))
plt.plot(bins,y1,ls="-",color="r")
plt.xticks([])
plt.yticks([])

# this is another inset axes over the main axes
plt.axes([0.65,0.6,0.2,0.2],fc="k")
count,bins,patches = plt.hist(y,bins,color="cornflowerblue",normed=True,cumulative=True,histtype="step")
plt.ylim(0,1.0)
plt.xticks([])
plt.yticks([])

# this is another inset axes over another inset axes
plt.axes([0.66,0.72,0.05,0.05])
y2 = (1/(sigma * np.sqrt(2 * np.pi))) * np.exp( - (bins - mu)**2 / (2 * sigma**2))
y2 = y2.cumsum()
y2 = y2/y2[-1]
plt.plot(bins,y2,ls="-",color="r")
plt.xticks([])
plt.yticks([])

plt.show()

4 设置文本内容的样式和布局

4.1 文本注解的样式展示

在这里插入图片描述

import matplotlib.patches as patches
import matplotlib.pyplot as plt

fig = plt.figure(1, figsize=(8,9), dpi=72)
fontsize = 0.5*fig.dpi

# subplot(111)
ax = fig.add_subplot(1,1,1,frameon=False,xticks=[],yticks=[])

boxStyles = patches.BoxStyle.get_styles()
boxStyleNames = list(boxStyles.keys())
boxStyleNames.sort()

for i,name in enumerate(boxStyleNames):
    ax.text(float(i+0.5)/len(boxStyleNames),
            (float(len(boxStyleNames))-0.5-i)/len(boxStyleNames),
            name,
            ha="center",
            size=fontsize,
            transform=ax.transAxes,
            bbox=dict(boxstyle=name,fc="w",ec="k"))

plt.show()

4.2文本注释箭头样式

在这里插入图片描述

import matplotlib.patches as patches
import matplotlib.pyplot as plt

fig = plt.figure(1, figsize=(8,9), dpi=72)
fontsize = 0.4*fig.dpi

# subplot(111)
ax = fig.add_subplot(1,1,1,frameon=False)

arrowStyles = patches.ArrowStyle.get_styles()
arrowStyleNames = list(arrowStyles.keys())
arrowStyleNames.sort()

ax.set_xlim(0,len(arrowStyleNames)+2.5)
ax.set_ylim(-2,len(arrowStyleNames))

for i,name in enumerate(arrowStyleNames):
    p = patches.Circle((float(len(arrowStyleNames))+1.2-i,float(len(arrowStyleNames))-2.8-i),
                       0.2,color="steelblue",alpha=1.0)
    ax.add_patch(p)
    ax.annotate(name,
                (float(len(arrowStyleNames))+1.2-i,float(len(arrowStyleNames))-2.8-i),
                (float(len(arrowStyleNames))-1-i,float(len(arrowStyleNames))-3-i),
                xycoords="data",
                ha="center",
                size=fontsize,
                arrowprops=dict(arrowstyle=name,
                                facecolor="k",
                                edgecolor="k",
                                patchB=p,
                                shrinkA=5,
                                shrinkB=5,
                                connectionstyle="arc3"),
                bbox=dict(boxstyle="round",fc="w",ec="k"))

ax.xaxis.set_visible(False)  # ax.set_xticks([])
ax.yaxis.set_visible(False)  # ax.set_yticks([])

plt.show()

4.3 文本内容布局

在这里插入图片描述

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

fig = plt.figure(1,figsize=(8,8),dpi=80,facecolor="w")
fontsize = 0.3*fig.dpi
font_style = {"family":"sans-serif","fontsize":fontsize,"weight":"black"}

# add axes in axis coords
ax = fig.add_axes([0.0,0.0,1.0,1.0],facecolor="gold")

left = 0.2
bottom = 0.2
right = 0.8
top = 0.8
width = right-left
height = top-bottom

# add a rectangle in axis coords
rect = Rectangle((left,bottom),
                 width,
                 height,
                 transform=ax.transAxes,
                 facecolor="w",
                 edgecolor="k")

ax.add_patch(rect)

# add text in axis coords
# left-bottom
ax.text(left,
        bottom,"left bottom",
        ha="left",
        va="bottom",
        transform=ax.transAxes,
        **font_style)

# left-top
ax.text(left,
        bottom,"left top",
        ha="left",
        va="top",
        transform=ax.transAxes,
        **font_style)

# right-bottom
ax.text(right,
        top,"right bottom",
        ha="right",
        va="bottom",
        transform=ax.transAxes,
        **font_style)

# right-top
ax.text(right,
        top,"right top",
        ha="right",
        va="top",
        transform=ax.transAxes,
        **font_style)

# center-top
ax.text(right,
        bottom,"center top",
        ha="center",
        va="top",
        transform=ax.transAxes,
        **font_style)

# center-bottom
ax.text(right,
        bottom,"center bottom",
        ha="center",
        va="bottom",
        transform=ax.transAxes,
        **font_style)

# left-center
ax.text(left,
        top,"left center",
        ha="left",
        va="center",
        transform=ax.transAxes,
        **font_style)

# right-center
ax.text(right,
        0.5,"right center",
        ha="right",
        va="center",
        transform=ax.transAxes,
        rotation=90,
        **font_style)

# center-center
ax.text(left,
        0.5,"center center",
        ha="center",
        va="center",
        transform=ax.transAxes,
        rotation=90,
        **font_style)

# middle
ax.text(0.5,
        0.5,"middle",
        ha="center",
        va="center",
        transform=ax.transAxes,
        color="r",
        **font_style)

# rotated center-center
# left-center
ax.text(left*0.7,
        top+0.05,"rotated\ncenter center",
        ha="center",
        va="center",
        transform=ax.transAxes,
        rotation=45,
        **font_style)

plt.show()

4.4 文本自动换行

在这里插入图片描述

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

fig = plt.figure(1,figsize=(8,8),dpi=80,facecolor="w")

# add axes in axis coords
ax = fig.add_axes([0.0,0.0,1.0,1.0],facecolor="gold")

left = 0.2
bottom = 0.2
right = 0.8
top = 0.8
width = right-left
height = top-bottom

the_Zen_of_Python = """
Explicit is better than implicit.Complex is better than complicated.
"""

# add a rectangle in axis coords
rect = Rectangle((left,bottom),
                 width,
                 height,
                 transform=ax.transAxes,
                 facecolor="w",
                 edgecolor="k")

ax.add_patch(rect)

ax.text(0.5,1,
        the_Zen_of_Python,
        transform=ax.transAxes,
        fontsize=18,
        weight="black",
        ha="center",
        va="top",
        wrap=True)

ax.text(0.5*width,
        0.5,
        the_Zen_of_Python,
        transform=ax.transAxes,
        ha="right",
        rotation=20,
        fontsize=10,
        style="italic",
        family="monospace",
        weight="black",
        wrap=True)

ax.text(left,
        bottom,
        the_Zen_of_Python,
        transform=ax.transAxes,
        ha="left",
        rotation=-15,
        fontsize=15,
        style="oblique",
        family="serif",
        weight="bold",
        wrap=True)

ax.text(width,
        0.5*height,
        the_Zen_of_Python,
        transform=ax.transAxes,
        ha="left",
        rotation=15,
        fontsize=22,
        style="normal",
        family="sans-serif",
        wrap=True)

plt.show()

4.5 文本旋转

在这里插入图片描述

import matplotlib.pyplot as plt

fig,ax = plt.subplots(1,2)

# subplot(121)

# ha-va:top
ax[0].text(0.5,2.5,"text45",ha="left",va="top",rotation=45,rotation_mode="default",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})
ax[0].text(1.5,2.5,"text45",ha="center",va="top",rotation=45,rotation_mode="default",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})
ax[0].text(2.5,2.5,"text45",ha="right",va="top",rotation=45,rotation_mode="default",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})

# ha-va:center
ax[0].text(0.5,1.5,"text45",ha="left",va="center",rotation=45,rotation_mode="default",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})
ax[0].text(1.5,1.5,"text45",ha="center",va="center",rotation=45,rotation_mode="default",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})
ax[0].text(2.5,1.5,"text45",ha="right",va="center",rotation=45,rotation_mode="default",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})

# ha-va:bottom
ax[0].text(0.5,0.5,"text45",ha="left",va="bottom",rotation=45,rotation_mode="default",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})
ax[0].text(1.5,0.5,"text45",ha="center",va="bottom",rotation=45,rotation_mode="default",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})
ax[0].text(2.5,0.5,"text45",ha="right",va="bottom",rotation=45,rotation_mode="default",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})

# set text point
ax[0].scatter([0.5,1.5,2.5,0.5,1.5,2.5,0.5,1.5,2.5],
              [2.5,2.5,2.5,1.5,1.5,1.5,0.5,0.5,0.5],
              c="r",
              s=50,
              alpha=0.5)

# ticklabel and tickline limit
ax[0].set_xticks([0.0,0.5,1.0,1.5,2.0,2.5,3.0])
ax[0].set_xticklabels(["","left","","center","","right",""],fontsize=15)
ax[0].set_yticks([3.0,2.5,2.0,1.5,1.0,0.5,0.0])
ax[0].set_yticklabels(["","top","","center","","bottom",""],rotation=90,fontsize=15)
ax[0].set_xlim(0.0,3.0)
ax[0].set_ylim(0.0,3.0)

ax[0].grid(ls="-",lw=2,color="b",alpha=0.5)

ax[0].set_title("default",fontsize=18)

# subplot(122)

# ha-va:top
ax[1].text(0.5,2.5,"text45",ha="left",va="top",rotation=45,rotation_mode="anchor",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})
ax[1].text(1.5,2.5,"text45",ha="center",va="top",rotation=45,rotation_mode="anchor",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})
ax[1].text(2.5,2.5,"text45",ha="right",va="top",rotation=45,rotation_mode="anchor",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})

# ha-va:center
ax[1].text(0.5,1.5,"text45",ha="left",va="center",rotation=45,rotation_mode="anchor",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})
ax[1].text(1.5,1.5,"text45",ha="center",va="center",rotation=45,rotation_mode="anchor",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})
ax[1].text(2.5,1.5,"text45",ha="right",va="center",rotation=45,rotation_mode="anchor",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})

# ha-va:bottom
ax[1].text(0.5,0.5,"text45",ha="left",va="bottom",rotation=45,rotation_mode="anchor",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})
ax[1].text(1.5,0.5,"text45",ha="center",va="bottom",rotation=45,rotation_mode="anchor",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})
ax[1].text(2.5,0.5,"text45",ha="right",va="bottom",rotation=45,rotation_mode="anchor",
           bbox={"boxstyle":"square","facecolor":"gray","edgecolor":"w","pad":0,"alpha":0.5})

# set text point
ax[1].scatter([0.5,1.5,2.5,0.5,1.5,2.5,0.5,1.5,2.5],
              [2.5,2.5,2.5,1.5,1.5,1.5,0.5,0.5,0.5],
              c="r",
              s=50,
              alpha=0.5)

# ticklabel and tickline limit
ax[1].set_xticks([0.0,0.5,1.0,1.5,2.0,2.5,3.0])
ax[1].set_xticklabels(["","left","","center","","right",""],fontsize=15)
ax[1].set_yticks([3.0,2.5,2.0,1.5,1.0,0.5,0.0])
ax[1].set_yticklabels(["","top","","center","","bottom",""],rotation=90,fontsize=15)
ax[1].set_xlim(0.0,3.0)
ax[1].set_ylim(0.0,3.0)

ax[1].grid(ls="-",lw=2,color="b",alpha=0.5)

ax[1].set_title("anchor",fontsize=18)

plt.show()

4.6 文本对齐方式

在这里插入图片描述

import matplotlib.pyplot as plt

fig,ax = plt.subplots(1,1)

# ha:left--va:baseline--multialignment: left,center, and right
ax.text(0.5,2.5,"text0\nTEXT0\nalignment",ha="left",va="baseline",multialignment="left",
           bbox=dict(boxstyle="square",facecolor="w",edgecolor="k",alpha=0.5))
ax.text(0.5,1.5,"text0\nTEXT0\nalignment",ha="left",va="baseline",multialignment="center",
           bbox=dict(boxstyle="square",facecolor="w",edgecolor="k",alpha=0.5))
ax.text(0.5,0.5,"text0\nTEXT0\nalignment",ha="left",va="baseline",multialignment="right",
           bbox=dict(boxstyle="square",facecolor="w",edgecolor="k",alpha=0.5))

# ha:center--va:baseline--multialignment: left,center, and right
ax.text(1.5,2.5,"text0\nTEXT0\nalignment",ha="center",va="baseline",multialignment="left",
           bbox=dict(boxstyle="square",facecolor="w",edgecolor="k",alpha=0.5))
ax.text(1.5,1.5,"text0\nTEXT0\nalignment",ha="center",va="baseline",multialignment="center",
           bbox=dict(boxstyle="square",facecolor="w",edgecolor="k",alpha=0.5))
ax.text(1.5,0.5,"text0\nTEXT0\nalignment",ha="center",va="baseline",multialignment="right",
           bbox=dict(boxstyle="square",facecolor="w",edgecolor="k",alpha=0.5))

# ha:right--va:baseline--multialignment: left,center, and right
ax.text(2.5,2.5,"text0\nTEXT0\nalignment",ha="right",va="baseline",multialignment="left",
           bbox=dict(boxstyle="square",facecolor="w",edgecolor="k",alpha=0.5))
ax.text(2.5,1.5,"text0\nTEXT0\nalignment",ha="right",va="baseline",multialignment="center",
           bbox=dict(boxstyle="square",facecolor="w",edgecolor="k",alpha=0.5))
ax.text(2.5,0.5,"text0\nTEXT0\nalignment",ha="right",va="baseline",multialignment="right",
           bbox=dict(boxstyle="square",facecolor="w",edgecolor="k",alpha=0.5))

# set text point
ax.scatter([0.5,1.5,2.5,0.5,1.5,2.5,0.5,1.5,2.5],
           [2.5,2.5,2.5,1.5,1.5,1.5,0.5,0.5,0.5],
           c="r",
           s=50,
           alpha=0.6)

# ticklabel and tickline limit
ax.set_xticks([0.0,0.5,1.0,1.5,2.0,2.5,3.0])
ax.set_xticklabels(["","left","","center","","right",""],fontsize=15)
ax.set_yticks([3.0,2.5,2.0,1.5,1.0,0.5,0.0])
ax.set_yticklabels(["","baseline","","baseline","","baseline",""],rotation=90,fontsize=15)
ax.set_xlim(0.0,3.0)
ax.set_ylim(0.0,3.0)

ax.grid(ls="-",lw=2,color="b",alpha=0.5)

plt.show()

4.7 连线

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

fig,ax = plt.subplots(3,5,figsize=(8, 5),sharex=True,sharey=True)

x1,y1 = 0.3,0.2
x2,y2 = 0.8,0.6

bbox = dict(boxstyle="square",facecolor="w",edgecolor="k")

# subplot(3,5,1)
ax[0,0].plot([x1,x2],[y1,y2],".")

ax[0,0].annotate("",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            arrowprops=dict(arrowstyle="->",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchA=None,
                            patchB=None,
                            connectionstyle="angle,angleA=0,angleB=90,rad=5"))

ax[0,0].text(0.05,0.95,"angle,\nangleA=0,\nangleB=90,\nrad=5",ha="left",va="top",bbox=bbox)

ax[0,0].set_xlim(0,1)
ax[0,0].set_ylim(0,1)
ax[0,0].set_xticklabels([])
ax[0,0].set_yticklabels([])

# subplot(3,5,6)
ax[1,0].plot([x1,x2],[y1,y2],".")

ax[1,0].annotate("",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            arrowprops=dict(arrowstyle="->",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchA=None,
                            patchB=None,
                            connectionstyle="angle,angleA=20,angleB=50,rad=10"))

ax[1,0].text(0.05,0.95,"angle,\nangleA=20,\nangleB=50,\nrad=10",ha="left",va="top",bbox=bbox)

ax[1,0].set_xlim(0,1)
ax[1,0].set_ylim(0,1)
ax[1,0].set_xticklabels([])
ax[1,0].set_yticklabels([])

# subplot(3,5,11)
ax[2,0].plot([x1,x2],[y1,y2],".")

ax[2,0].annotate("",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            arrowprops=dict(arrowstyle="->",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchA=None,
                            patchB=None,
                            connectionstyle="angle,angleA=-90,angleB=0,rad=0.0"))

ax[2,0].text(0.05,0.95,"angle,\nangleA=-90,\nangleB=0,\nrad=0.0",ha="left",va="top",bbox=bbox)

ax[2,0].set_xlim(0,1)
ax[2,0].set_ylim(0,1)
ax[2,0].set_xticklabels([])
ax[2,0].set_yticklabels([])

# subplot(3,5,2)
ax[0,1].plot([x1,x2],[y1,y2],".")

ax[0,1].annotate("",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            arrowprops=dict(arrowstyle="->",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchA=None,
                            patchB=None,
                            connectionstyle="angle3,angleA=90,angleB=0"))

ax[0,1].text(0.05,0.95,"angle3,\nangleA=90,\nangleB=0",ha="left",va="top",bbox=bbox)

ax[0,1].set_xlim(0,1)
ax[0,1].set_ylim(0,1)
ax[0,1].set_xticklabels([])
ax[0,1].set_yticklabels([])

# subplot(3,5,7)
ax[1,1].plot([x1,x2],[y1,y2],".")

ax[1,1].annotate("",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            arrowprops=dict(arrowstyle="->",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchA=None,
                            patchB=None,
                            connectionstyle="angle3,angleA=0,angleB=90"))

ax[1,1].text(0.05,0.95,"angle3,\nangleA=0,\nangleB=90",ha="left",va="top",bbox=bbox)

ax[1,1].set_xlim(0,1)
ax[1,1].set_ylim(0,1)
ax[1,1].set_xticklabels([])
ax[1,1].set_yticklabels([])

# subplot(3,5,12)
ax[2,1].plot([x1,x2],[y1,y2],".")

ax[2,1].annotate("",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            arrowprops=dict(arrowstyle="->",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchA=None,
                            patchB=None,
                            connectionstyle="angle3,angleA=20,angleB=60"))

ax[2,1].text(0.05,0.95,"angle3,\nangleA=20,\nangleB=60",ha="left",va="top",bbox=bbox)

ax[2,1].set_xlim(0,1)
ax[2,1].set_ylim(0,1)
ax[2,1].set_xticklabels([])
ax[2,1].set_yticklabels([])

# subplot(3,5,3)
ax[0,2].plot([x1,x2],[y1,y2],".")

ax[0,2].annotate("",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            arrowprops=dict(arrowstyle="->",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchA=None,
                            patchB=None,
                            connectionstyle="arc,angleA=-90,armA=30,angleB=0,armB=30,rad=0.0"))

ax[0,2].text(0.05,0.95,"arc,\nangleA=-90,\narmA=30,\nangleB=0,\narmB=30,\nrad=0.0",ha="left",va="top",bbox=bbox)

ax[0,2].set_xlim(0,1)
ax[0,2].set_ylim(0,1)
ax[0,2].set_xticklabels([])
ax[0,2].set_yticklabels([])

# subplot(3,5,8)
ax[1,2].plot([x1,x2],[y1,y2],".")

ax[1,2].annotate("",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            arrowprops=dict(arrowstyle="->",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchA=None,
                            patchB=None,
                            connectionstyle="arc,angleA=-90,armA=30,angleB=0,armB=30,rad=3"))

ax[1,2].text(0.05,0.95,"arc,\nangleA=-90,\narmA=30,\nangleB=0,\narmB=30,\nrad=3",ha="left",va="top",bbox=bbox)

ax[1,2].set_xlim(0,1)
ax[1,2].set_ylim(0,1)
ax[1,2].set_xticklabels([])
ax[1,2].set_yticklabels([])

# subplot(3,5,13)
ax[2,2].plot([x1,x2],[y1,y2],".")

ax[2,2].annotate("",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            arrowprops=dict(arrowstyle="->",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchA=None,
                            patchB=None,
                            connectionstyle="arc,angleA=100,armA=0,angleB=20,armB=50,rad=10"))

ax[2,2].text(0.05,0.95,"arc,\nangleA=100,\narmA=0,\nangleB=20,\narmB=50,\nrad=10",ha="left",va="top",bbox=bbox)

ax[2,2].set_xlim(0,1)
ax[2,2].set_ylim(0,1)
ax[2,2].set_xticklabels([])
ax[2,2].set_yticklabels([])

# subplot(3,5,4)
ax[0,3].plot([x1,x2],[y1,y2],".")

ax[0,3].annotate("",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            arrowprops=dict(arrowstyle="->",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchA=None,
                            patchB=None,
                            connectionstyle="arc3,rad=0.0"))

ax[0,3].text(0.05,0.95,"arc3,\nrad=0.0",ha="left",va="top",bbox=bbox)

ax[0,3].set_xlim(0,1)
ax[0,3].set_ylim(0,1)
ax[0,3].set_xticklabels([])
ax[0,3].set_yticklabels([])

# subplot(3,5,9)
ax[1,3].plot([x1,x2],[y1,y2],".")

ax[1,3].annotate("",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            arrowprops=dict(arrowstyle="->",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchA=None,
                            patchB=None,
                            connectionstyle="arc3,rad=0.5"))

ax[1,3].text(0.05,0.95,"arc3,\nrad=0.5",ha="left",va="top",bbox=bbox)

ax[1,3].set_xlim(0,1)
ax[1,3].set_ylim(0,1)
ax[1,3].set_xticklabels([])
ax[1,3].set_yticklabels([])

# subplot(3,5,14)
ax[2,3].plot([x1,x2],[y1,y2],".")

ax[2,3].annotate("",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            arrowprops=dict(arrowstyle="->",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchA=None,
                            patchB=None,
                            connectionstyle="arc3,rad=-0.5"))

ax[2,3].text(0.05,0.95,"arc3,\nrad=-0.5",ha="left",va="top",bbox=bbox)

ax[2,3].set_xlim(0,1)
ax[2,3].set_ylim(0,1)
ax[2,3].set_xticklabels([])
ax[2,3].set_yticklabels([])

# subplot(3,5,5)
ax[0,4].plot([x1,x2],[y1,y2],".")

ax[0,4].annotate("",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            arrowprops=dict(arrowstyle="->",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchA=None,
                            patchB=None,
                            connectionstyle="bar,armA=30,armB=30,fraction=0.0"))

ax[0,4].text(0.05,0.95,"bar,\narmA=30,\narmB=30,\nfraction=0.0",ha="left",va="top",bbox=bbox)

ax[0,4].set_xlim(0,1)
ax[0,4].set_ylim(0,1)
ax[0,4].set_xticklabels([])
ax[0,4].set_yticklabels([])

# subplot(3,5,10)
ax[1,4].plot([x1,x2],[y1,y2],".")

ax[1,4].annotate("",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            arrowprops=dict(arrowstyle="->",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchA=None,
                            patchB=None,
                            connectionstyle="bar,fraction=-0.3"))

ax[1,4].text(0.05,0.95,"bar,\nfraction=-0.3",ha="left",va="top",bbox=bbox)

ax[1,4].set_xlim(0,1)
ax[1,4].set_ylim(0,1)
ax[1,4].set_xticklabels([])
ax[1,4].set_yticklabels([])

# subplot(3,5,15)
ax[2,4].plot([x1,x2],[y1,y2],".")

ax[2,4].annotate("",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            arrowprops=dict(arrowstyle="->",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchA=None,
                            patchB=None,
                            connectionstyle="bar,armB=60,angle=90,fraction=0.4"))

ax[2,4].text(0.05,0.95,"bar,\narmB=60,\nangle=90,\nfraction=0.4",ha="left",va="top",bbox=bbox)

ax[2,4].set_xlim(0,1)
ax[2,4].set_ylim(0,1)
ax[2,4].set_xticklabels([])
ax[2,4].set_yticklabels([])

fig.subplots_adjust(left=0.05,right=0.95,bottom=0.05,top=0.95,wspace=0.02,hspace=0.02)

plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

fig,ax = plt.subplots(1,3,figsize=(3,3),sharex=True,sharey=True)

x1,y1 = 0.3,0.3
x2,y2 = 0.7,0.7

bbox = dict(boxstyle="square",facecolor="w",edgecolor="k")

# subplot(131)
ax[0].annotate("simple",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            bbox=dict(boxstyle="round",fc="w",ec="k"),
            arrowprops=dict(arrowstyle="simple,head_length=0.7,head_width=0.6,tail_width=0.3",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchB=None,
                            connectionstyle="angle3,angleA=0,angleB=90"),
               size=25,
               ha="center",
               va="center")

ax[0].text(0.05,0.95,"angle3,\nangleA=0,\nangleB=90",ha="left",va="top",bbox=bbox,size=20)

ax[0].set_xlim(0,1)
ax[0].set_ylim(0,1)
ax[0].set_xticklabels([])
ax[0].set_yticklabels([])

# subplot(132)
ax[1].annotate("fancy",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            bbox=dict(boxstyle="round",fc="w",ec="k"),
            arrowprops=dict(arrowstyle="fancy,head_length=0.4,head_width=0.4,tail_width=0.6",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchB=None,
                            connectionstyle="arc3,rad=0.5"),
               size=25,
               ha="center",
               va="center")

ax[1].text(0.05,0.95,"arc3,\nrad=0.5",ha="left",va="top",bbox=bbox,size=20)

ax[1].set_xlim(0,1)
ax[1].set_ylim(0,1)
ax[1].set_xticklabels([])
ax[1].set_yticklabels([])

# subplot(133)
ax[2].annotate("wedge",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            bbox=dict(boxstyle="round",fc="w",ec="k"), 
            arrowprops=dict(arrowstyle="wedge,tail_width=0.5",
                            color="gray",
                            shrinkA=5,
                            shrinkB=5,
                            patchB=None,
                            connectionstyle="arc3,rad=-0.3"),
               size=25,
               ha="center",
               va="center")

ax[2].text(0.05,0.95,"arc3,\nrad=-0.3",ha="left",va="top",bbox=bbox,size=20)

ax[2].set_xlim(0,1)
ax[2].set_ylim(0,1)
ax[2].set_xticklabels([])
ax[2].set_yticklabels([])

fig.subplots_adjust(left=0.05,right=0.95,bottom=0.05,top=0.95,wspace=0.02,hspace=0.02)

plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

fig,ax = plt.subplots(1,3,figsize=(3,3),sharex=True,sharey=True)

x1,y1 = 0.2,0.2
x2,y2 = 0.8,0.8

bbox = dict(boxstyle="square",facecolor="w",edgecolor="k")

# subplot(131)
ax[0].annotate("relpos=(0.0,0.0)",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            bbox=dict(boxstyle="round4",fc="w",ec="k"),
            arrowprops=dict(arrowstyle="-|>",
                            color="k",
                            relpos=(0.0,0.0),
                            connectionstyle="arc3,rad=0.3"),
               size=15,
               ha="center",
               va="center")

ax[0].text(0.05,0.95,"arc3,\nrad=0.3",ha="left",va="top",bbox=bbox,size=20)

ax[0].set_xlim(0,1)
ax[0].set_ylim(0,1)
ax[0].set_xticklabels([])
ax[0].set_yticklabels([])

# subplot(132)
ax[1].annotate("relpos=(1.0,0.0)",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            bbox=dict(boxstyle="round4",fc="w",ec="k"),
            arrowprops=dict(arrowstyle="-|>",
                            color="k",
                            relpos=(1.0,0.0),
                            connectionstyle="arc3,rad=-0.3"),
               size=15,
               ha="center",
               va="center")

ax[1].text(0.05,0.95,"arc3,\nrad=-0.3",ha="left",va="top",bbox=bbox,size=20)

ax[1].set_xlim(0,1)
ax[1].set_ylim(0,1)
ax[1].set_xticklabels([])
ax[1].set_yticklabels([])

# subplot(133)
ax[2].annotate("relpos=(0.2,0.8)",
            xy=(x1,y1),xycoords="data",
            xytext=(x2,y2),textcoords="data",
            bbox=dict(boxstyle="round4",fc="w",ec="k"), 
            arrowprops=dict(arrowstyle="-|>",
                            color="k",
                            relpos=(0.2,0.8),
                            connectionstyle="arc3,rad=-0.3"),
               size=15,
               ha="center",
               va="center")

ax[2].text(0.05,0.95,"arc3,\nrad=-0.3",ha="left",va="top",bbox=bbox,size=20)

ax[2].set_xlim(0,1)
ax[2].set_ylim(0,1)
ax[2].set_xticklabels([])
ax[2].set_yticklabels([])

fig.subplots_adjust(left=0.05,right=0.95,bottom=0.05,top=0.95,wspace=0.02,hspace=0.02)

plt.show()

5.调整计量单位和计量方法

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值