python_matplotlib常用技巧

主要常用技巧

  • 中文显示
  • 子图的间距
  • 子图的边框
  • x轴标签:xtickes (任意间隔)
  • 数据标注:text(任意间隔)
  • 任意子图大小

一、中文显示问题

import matplotlib.pyplot as plt
# plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] # mac 
plt.rcParams['font.sans-serif'] =['SimHei'] # widows
plt.rcParams['axes.unicode_minus'] = False # 负号

一、子图间距与边框

  • 子图间距
fig, axes = plt.subplots(1, 2, figsize=(16, 8))
plt.subplots_adjust(wspace=1) # 间距 wspace hspace
  • 有边框
sns.violinplot(x=df['target'], y=df['petal_length'], ax=axes[0])
sns.swarmplot(x=df['target'], y=df['petal_length'], ax=axes[0], size=3, linewidth=0.5)
axes[0].set_title('普通带有边框$violinplot+swarmplot$, wspace=1')
  • 无边框

删除三边,仅仅保留底边

sns.violinplot(x=df['target'], y=df['petal_length'], ax=axes[1])
axes[1].set_title('普通带有无框$violinplot$, wspace=1')
for i in ['top', 'left', 'right', 'bottom']: # 设置边框
    if i in ['bottom']:
        axes[1].spines[i].set_linewidth('1.5')
    else:
        axes[1].spines[i].set_visible(False)
plt.show()

wspace=1
(图:wspace=1)

wspace=0.2
(图:wspace=0.2)

二、X轴标注和数据标注(任意间隔)

x轴的数据是连续的,如果是文本则是从0开始的整数列表。我们只需要选出,我们想要标注的地方,在这个地方用自己想要标注的信息标注就可以。

# x轴上的点采样
xtick_list = list(range(0, len(df.petal_length), 10))
# 对于位置想要标注的label
xtick_labels =[f'label{i}' for i in xtick_list]
plt.xticks( xtick_list ,  xtick_labels)

在这里插入图片描述
同样的方法,我们也可以用在数字标注上。

plt.figure(figsize=(16, 8))
plt.style.use('ggplot')
plt.plot(df.petal_length)
# 间隔10个标注x
xtick_list = list(range(0, len(df.petal_length), 10))
xtick_labels =[f'label{i}' for i in xtick_list]
plt.xticks(
    xtick_list ,  xtick_labels
)
# 同样的方法也可以用于x的标注
for x, y in zip(xtick_list, [df.petal_length.tolist()[i] for i in xtick_list]):
    plt.text(x, y+0.1, y ,va='center', ha='center', fontdict={'fontsize':13, 'color': 'steelblue'})
plt.show()

在这里插入图片描述

三、任意子图大小

主要步骤:
  1. 准备画布,并允许画布上网格连续:fig=plt.figure(constrained_layout=True)
  2. 给画布划分网格空间: gs = fig.add_gridspec(3, 3)
  3. 在画布上选取指定地方 :axe = fig.add_subplot(gs[1, :]) 画第二行
  4. 在指定地方画图: axe.plot()

3.1 按步骤简单示例

# 1 准备画布,并允许画布上网格连续:
fig = plt.figure(constrained_layout=True)
# 2. 给画布划分网格空间: 
gs = fig.add_gridspec(3, 3)
# 3. 在画布上选取指定地方 :画第二行
axe1 = fig.add_subplot(gs[1, :])
# 4. 在指定地方画图: 
axe1.plot(list(range(10)))
axe1.set_title('gs[1, :]')
plt.show()

在这里插入图片描述

3.2 iris的一个示例

def quick_plot(axes):
    sns.violinplot(x=df['target'], y=df['petal_length'], ax=axes)

fig = plt.figure(constrained_layout=True, figsize=(11,9))
gs = fig.add_gridspec(3, 3)
plt.subplots_adjust(hspace=0.4, wspace=0.2)
f_ax00, f_ax01 = fig.add_subplot(gs[0, 0]), fig.add_subplot(gs[0, 1:]) #, fc='#F5DEB3', alpha=0.6)
f_ax22 = fig.add_subplot(gs[1:, :])
quick_plot(f_ax00)
quick_plot(f_ax01)
quick_plot(f_ax22)
f_ax00.set_title('gs[0, 0]')
f_ax01.set_title('gs[0, 1:]')
f_ax22.set_title('gs[1:, :]')
plt.show()

在这里插入图片描述

参考

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Scc_hy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值