文章目录
老规矩,首先import:
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
常用操作
设置画布
figsize为画布尺寸,dpi为像素。
plt.figure(figsize=(,),dpi=)
设置风格
print(plt.style.available) # 查看已有风格
# ['Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background', 'fast',
# 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind',
# 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted',
# 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk',
# 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10']
plt.style.use('dark_background') # 风格调用
设置子图
# 两种方法:
# 方法一:
ax1 = fig.add_subplot(222) # 设置i行j列,指定在第k个位置
ax1.set(xlim=[0.5, 4.5], ylim=[-2, 8], title='An Example Axes',
ylabel='Y-Axis', xlabel='X-Axis') # 设置x轴、y轴范围
plt.show()
# 方法二:
plt.subplot(211)# 211 表示一会要画的图是2行一列的 最后一个1表示的是子图当中的第1个图
plt.plot(x,y,color='r')
plt.show()
# 方法三
fig, axes = plt.subplots(nrows=1, ncols=2) # 两子图左右并列
v_bars = axes[0].bar(x, y, color='red') # 竖着画条形图
h_bars = axes[1].barh(x, y, color='red') # 横着画条形图
子图布局操作
划分区块
- plt.subplot2grid((总有几行,总有几列),(起始行位置,起始列位置),rowspan=占几行,colspan=占几列)
ax1 = plt.subplot2grid((3,3),(0,0))
ax2 = plt.subplot2grid((3,3),(1,0))
ax3 = plt.subplot2grid((3,3),(0,2),rowspan=3)
ax4 = plt.subplot2grid((3,3),(2,0),colspan=2)
ax5 = plt.subplot2grid((3,3),(0,1),rowspan=2)
嵌套子图
add_axes
- left对应着嵌套图的最左边起始位置,bottom为最下边起始位置,width代表嵌套子图宽度,height代表嵌套子图高度。
fig = plt.figure()
ax = fig.add_subplot(111)
left, bottom, width, height = [0.22, 0.4, 0.3, 0.35]
ax1 = fig.add_axes([left, bottom, width, height])
# fig.add_axes在原图上增加一个轴
x = np.linspace(0, 10, 1000)
y1 = x ** 2
y2 = np.sin(x ** 2)
ax.plot(x, y1)
ax1.plot(x, y2)
plt.show()
insert_axes
top10_arrivals_countries = ['CANADA', 'MEXICO', 'UNITED\nKINGDOM', 'JAPAN', 'CHINA', 'GERMANY', 'SOUTH\nKOREA',
'FRANCE', 'BRAZIL', 'AUSTRALIA']
top10_arrivals_values = [16.625687, 15.378026, 3.934508, 2.999718, 2.618737, 1.769498, 1.628563, 1.419409, 1.393710,
1.136974]
arrivals_countries = ['WESTERN\nEUROPE', 'ASIA', 'SOUTH\nAMERICA', 'OCEANIA', 'CARIBBEAN', 'MIDDLE\nEAST',
'CENTRAL\nAMERICA', 'EASTERN\nEUROPE', 'AFRICA']
arrivals_percent = [36.9, 30.4, 13.8, 4.4, 4.0, 3.6, 2.9, 2.6, 1.5]
# 画一个bar图
fig, ax1 = plt.subplots(figsize=(20, 12))
bar1 = ax1.bar(range(10), top10_arrivals_values, color='pink')
plt.xticks(range(10), top10_arrivals_countries, fontsize=18)
def autolabel(bar_fig):
for bar in bar_fig:
height = bar.get_height()
ax1.text(bar.get_x() + bar.get_width() / 2., 0.95 * height, # 设定数据标签的xy坐标
'{:.2f}'.format(height), ha='center', fontsize=13, color='grey') # 设定对其方式
autolabel(bar1)
# 嵌套一个饼图
ax2 = inset_axes(ax1, width=6, height=6, loc='upper right')
explode = [0.06, 0.06, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05]
patches, texts, autotexts = ax2.pie(arrivals_percent, labels=arrivals_countries, autopct='%.1f%%', explode=explode)
for text in texts:
text.set_fontsize(13)
for text in autotexts:
text.set_fontsize(10)
text.set_color('white')
plt.show()
坐标轴操作
坐标轴标题
plt.xlabel('xlabel',fontsize = 16)
plt.ylabel('ylabel')
坐标轴范围
plt.xlim()
ply.ylim()
坐标轴刻度
plt.xticks([],[]) # x的刻度——数字刻度&对应标识
plt.yticks([],[])
设置汉字和负数
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
调整刻度、边框位置
ax.spines['right'].set_color('none') # spines获取边框,set_color设置颜色
ax.spines['top'].set_visible(False) # 去掉边框
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_position(('data', 1000)) # 设置轴位置;位置所有属性:outward,axes