from matplotlib import pyplot as plt
import numpy as np
dates = np.arange(1991,2021)
dates
#array([1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
# 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
# 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020])
sales = np.random.randint(50,500,size=30)
sales
#array([150, 115, 52, 247, 113, 54, 204, 405, 245, 438, 251, 392, 222,
# 416, 388, 112, 250, 473, 444, 195, 147, 123, 136, 294, 240, 129,
# 290, 255, 381, 149])
plt.plot(dates,sales)

%matplotlib inline
plt.xticks([1980,1982,1993])
plt.plot(dates,sales)

- %matplotlib notebook
plt.xticks([1980,1982,1993])
plt.plot(dates,sales)

plt.xticks([1990,2005,2010,2020])
plt.plot(dates,sales)

plt.xticks(np.arange(dates.min(),dates.max()+2,2),rotation=45)
plt.plot(dates,sales)

那么,对于上述的逻辑分割,我们也是可以使用元素本身的,只不过需要一些计算。
plt.xticks([dates[i] for i in range(0,len(dates),2)]+[2020],rotation=45) # 元素本身
plt.plot(dates,sales)

dates = np.arange(1991,2021).astype(np.str_)
plt.xticks(range(1,len(dates),2),rotation=45) # 元素本身
plt.plot(dates,sales)

time=np.arange(2000,2020).astype(np.str_)
sales = [109, 150, 172, 260, 273, 333, 347, 393, 402, 446, 466, 481, 499,504, 513, 563, 815, 900, 930, 961]
plt.xticks(range(0,len(time),2))##,labels=['year%s'%i for i in time],rotation=45,color="red")
plt.yticks(color="blue")
plt.plot(time,sales)

plt.grid(True, linestyle = "--",color = "gray", linewidth = "0.5",axis = 'x')
x = np.linspace(-np.pi,np.pi,256,endpoint = True)
c, s = np.cos(x), np.sin(x)
plt.plot(x, c)
plt.plot(x, s)
plt.grid(True,linestyle="--")


x = np.arange(-50,51)
y = x ** 2
plt.plot(x, y)

x = np.arange(-50,51)
y = x ** 2
ax = plt.gca()
ax.spines['right'].set_color("none")
ax.spines['top'].set_color("none")
plt.plot(x, y)

x = np.arange(-50,51)
y = x ** 2
ax = plt.gca()
ax.spines['right'].set_color("none")
ax.spines['top'].set_color("none")
ax.spines['left'].set_position(('axes',0.5))
plt.ylim(0, y.max())
plt.plot(x, y)

plt.plot()

plt.rcParams['figure.figsize'] = (6.0, 4.0)
plt.plot()

plt.rcParams['figure.dpi'] = 100
plt.plot()

plt.rcParams['figure.figsize']=(3,2)
plt.plot()

plt.plot(x,y,color='red',alpha=0.3,linestyle='-',linewidth=5,marker='o',markeredgecolor='r',markersize='20',markeredgewidth=10)
| 字符 | 颜色 | 英文全称 |
|---|---|---|
| ‘b’ | 蓝色 | blue |
| ‘g’ | 绿色 | green |
| ’ r ’ | 红色 | red |
| ’ c ’ | 青色 | cyan |
| ’ m ’ | 品红 | magenta |
| ’ y ’ | 黄色 | yellow |
| ’ k ’ | 黑色 | black |
| ’ w ’ | 白色 | white |
| 字符 | 描述 |
|---|---|
| ‘-’ | 实线 |
| ‘–’ | 虚线 |
| ‘-.’ | 点划线 |
| ‘:’ | 虚线 |
| 标记符号 | 描述 |
|---|---|
| ‘.’ | 点标记 |
| ‘o’ | 圆圈标记 |
| ‘x’ | 'X’标记 |
| ‘D’ | 钻石标记 |
| ‘H’ | 六角标记 |
| ‘s’ | 正方形标记 |
| ‘+’ | 加号标记 |
x= np.arange(0, 100,10)
y= x ** 2
"""linewidth 设置线条粗细label 设置线条标签color 设置线条颜色linestyle 设置线条形状marker 设置线条样点标记
"""
plt.plot(x, y, linewidth = '2', label = "test", color='b', linestyle='--', marker='H')
plt.legend(loc='upper left')

plt.plot([1,2,3],[4,7,6],'r*-.')
plt.plot([2,4,5],[3,8,7],'m+--')

plt.rcParams['figure.figsize']=(8,4)
x=np.linspace(0,10,100)
plt.plot(x,x+0, '-g', label='-g')
plt.plot(x,x+1, '--c', label='--c')
plt.plot(x,x+2, '-.k', label='-.k')
plt.plot(x,x+3, '-r', label='-r')
plt.plot(x,x+4, 'o', label='o')
plt.plot(x,x+5, 'x', label='x')
plt.plot(x,x+6, 'dr', label='dr')
plt.legend(loc='lower right',framealpha=0.5,shadow=True, borderpad=0.5)

plt.figure(num=None,figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, **kwargs)
from matplotlib import pyplot as plt
fig = plt.figure()
fig = plt.figure('f1',figsize=(3,2),dpi=100)
x = np.arange(0,50)
y = x ** 2
plt.plot(x,y)

x = np.arange(0,50)
y = x ** 2
fig = plt.figure('f1',figsize=(4,2), dpi=100,facecolor='gray')
ax = plt.gca()
ax.plot(x,y)
plt.plot(x,y)

add_axes(rect)
fig = plt.figure(figsize=(4,2),facecolor='g')
ax1=fig.add_axes([0,0,1,1])
ax2=fig.add_axes([0.1,0.6,0.3,0.3])
ax3=fig.add_axes([0.5,0.6,0.2,0.3])
ax1.plot(x, y)
ax2.plot(x, y)
ax3.plot(x, y)

fig = plt.figure(figsize=(4,2),facecolor='g')
x = np.arange(0,50,2)
y = x ** 2
ax1 = fig.add_axes([0.0,0.0,1,1])
ax1.plot(x,y)
ax2=fig.add_axes([0.4,0.4,0.3,0.3])
ax2.plot(x,y)

ax = plt.subplot(nrows, ncols, index,*args, **kwargs)

plt.plot([1,2,3])
plt.subplot(211)
plt.plot(range(50,70))
plt.subplot(212)
plt.plot(np.arange(12)**2)
- 如果不想覆盖之前的图,需要先创建画布。还可以先设置画布的大小,再通过画布创建区域。
fig = plt.figure(figsize=(4,2))
fig.add_subplot(111)
plt.plot(range(20))
fig.add_subplot(221)
plt.plot(range(12))

plt.subplot(211,title="pic1", xlabel="x axis")
plt.plot(range(50,70))
plt.subplot(212, title="pic2", xlabel="x axis")
plt.plot(np.arange(12)**2)

plt.subplot(211,title="pic1", xlabel="x axis")
plt.plot(range(50,70))
plt.subplot(212, title="pic2", xlabel="x axis")
plt.plot(np.arange(12)**2)
plt.tight_layout()

plt.subplot(211)
plt.title("ax1")
plt.plot(range(50,70))
plt.subplot(212)
plt.title("ax2")
plt.plot(np.arange(12)**2)
plt.tight_layout()

ax1 = plt.subplot(211)
ax1.set_title("ax1")
ax1.plot(range(50,70))
ax2 = plt.subplot(212)
ax2.set_title("ax2")
ax2.plot(np.arange(12)**2)
plt.tight_layout()

fig , ax = plt.subplots(nrows, ncols)
import matplotlib.pyplot as plt
import numpy as np
fig, axes = plt.subplots(2,2)
x = np.arange(1,5)
axes[0][0].plot(x, x*x)
axes[0][0].set_title('square')
axes[0][1].plot(x, np.sqrt(x))
axes[0][1].set_title('square root')
axes[1][0].plot(x, np.exp(x))
axes[1][0].set_title('exp')
axes[1][1].plot(x,np.log10(x))
axes[1][1].set_title('log')
plt.tight_layout()
plt.show()
