python X/Y轴缩放

需求

参考官网:Scales — Matplotlib 3.4.3 documentation

常规画图时,有时候对X轴或者Y轴,希望某个区域可以放大,某个区域可以缩小。

方法:

可以尝试ax.set_xscale()  ax.set_yscale()

目前已有的方法是:"linear", "log", "symlog", "logit"

但是除了以上,还可以自定义函数

实例:

X轴范围0-100,希望一半区域是0-10,另外一半是10-100

forward和inverse是比例转换及其逆的方法(具体这里为啥是m³,我还是不明白,但是勉强可以实现自己的目的)

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter, FixedLocator

def forward(x):
    return x**(1/3)


def inverse(x):
    return x**3

data = np.random.randint(0,100,size=100)

sns.set(style='dark')
fig, axs = plt.subplots(1, 1 ,figsize=(6, 6),constrained_layout=True)
ax = axs

ax.violinplot(data, showmeans=False, showmedians=False, vert=False, showextrema=False)
ax.boxplot(data, vert=False, showfliers=False)

ax.axvline(0, -1, 1, linestyle='-', color='grey', linewidth=0.5)
ax.axvline(10, -1, 1, linestyle='-', color='grey', linewidth=0.5)
ax.axvline(100, -1, 1, linestyle='-', color='grey', linewidth=0.5)

##x轴缩放设置
ax.set_xscale("function",functions=(forward, inverse))
ax.xaxis.set_major_locator(FixedLocator((0,10,100)))

###设置XY轴标签
plt.xlabel("test")
plt.ylabel("hhh")
plt.xlim(0,101)

###去掉边框线
ax = plt.subplot()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
plt.show()

结果:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

晏九

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

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

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

打赏作者

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

抵扣说明:

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

余额充值