I want to make a plot with square root scale using Python:

However, I have no idea how to make it. Matplotlib allows to make log scale but in this case I need something like power function scale.

解决方案

You can make your own ScaleBase class to do this. I have modified the example from here (which made a square-scale, not a square-root-scale) for your purposes. Also, see the documentation here.

Note that to do this properly, you should probably also create your own custom tick locator; I haven't done that here though; I just manually set the major and minor ticks using ax.set_yticks().

import matplotlib.scale as mscale

import matplotlib.pyplot as plt

import matplotlib.transforms as mtransforms

import matplotlib.ticker as ticker

import numpy as np

class SquareRootScale(mscale.ScaleBase):

"""

ScaleBase class for generating square root scale.

"""

name = 'squareroot'

def __init__(self, axis, **kwargs):

# note in older versions of matplotlib (<3.1), this worked fine.

# mscale.ScaleBase.__init__(self)

# In newer versions (>=3.1), you also need to pass in `axis` as an arg

mscale.ScaleBase.__init__(self, axis)

def set_default_locators_and_formatters(self, axis):

axis.set_major_locator(ticker.AutoLocator())

axis.set_major_formatter(ticker.ScalarFormatter())

axis.set_minor_locator(ticker.NullLocator())

axis.set_minor_formatter(ticker.NullFormatter())

def limit_range_for_scale(self, vmin, vmax, minpos):

return max(0., vmin), vmax

class SquareRootTransform(mtransforms.Transform):

input_dims = 1

output_dims = 1

is_separable = True

def transform_non_affine(self, a):

return np.array(a)**0.5

def inverted(self):

return SquareRootScale.InvertedSquareRootTransform()

class InvertedSquareRootTransform(mtransforms.Transform):

input_dims = 1

output_dims = 1

is_separable = True

def transform(self, a):

return np.array(a)**2

def inverted(self):

return SquareRootScale.SquareRootTransform()

def get_transform(self):

return self.SquareRootTransform()

mscale.register_scale(SquareRootScale)

fig, ax = plt.subplots(1)

ax.plot(np.arange(0, 9)**2, label='$y=x^2$')

ax.legend()

ax.set_yscale('squareroot')

ax.set_yticks(np.arange(0,9,2)**2)

ax.set_yticks(np.arange(0,8.5,0.5)**2, minor=True)

plt.show()

python开根号函数图像,使用matplotlib / python的平方根刻度相关推荐

  1. 怎么用python画sin函数图像_用python绘制函数图像

    需要的库:matplotlib 用法: importmatplotlib.pyplot as plt plt.plot(x, y) #x, y是两个列表 plt.show() 例子: importma ...

  2. 用python画分段函数图像_使用Python拟合分段函数

    我试图用Numpy来拟合分段函数和绝对值.在 数学函数是 x&lt:p[1]:y=1+p[0]*abs((size+x-p[1])/size-size/2) x&gt:=p[1]:y= ...

  3. Python实现绘制函数图像——以Sigmoid函数为例

    在深度学习的研究中,我们经常需要知道激活函数(阶跃函数)的图像,以此判断该神经网络的阈值,并更好的去对权重进行调整.但对于某些复杂的复合函数而言,我们非常困难手画出它的函数图像,这样不仅费时费力,而且 ...

  4. python实现绘制函数图像

    目录 python实现绘制二次函数图像 python实现绘制三维函数图像 python实现绘制二次函数图像 import matplotlib.pyplot as plt import numpy a ...

  5. 如何用python画函数曲线_python 画函数曲线示例 用python 怎么画函数图像

    Python如何画函数的曲线 输入以下代码导入小编们用到的函数库. >>> import numpy as np >>> import matplotlib.pyp ...

  6. python实现数学函数图像

    python实现数学函数图像 预备知识 使用python绘制基本初等函数 在数学里,基本初等函数包括幂函数.指数函数.对数函数.三角函数.反三角函数.常数函数.不同函数的定义: python标准库[注 ...

  7. 使用python绘制任意函数图像

    使用python绘制任意函数图像 ##原创文章不易,望点赞转发收藏三连 效果展示 y = (sinx)/x y = x4+x3+x^2+1 y=-x^2 文末还会展示一些实例图片 引入需要的库 我们使 ...

  8. 怎么用python画sin函数图像_如何使用python的matplotlib模块画正弦函数图像

    python是一个很有趣的语言,可以在命令行窗口运行.python中有很多功能强大的模块,这篇经验告诉你,如何利用python的matplotlib模块,绘制正弦函数y=sin(x)的图像. 工具/原 ...

  9. maya python 开根号_maya python

    胡泳滨MayaPython简易教程,如需转载,请标明出处地址: http://huyongbin.blogbus.com/c3363976/ 谢谢配合! MayaPython第一篇 - 介绍 大家好, ...

最新文章

  1. Android系统Surface机制的SurfaceFlinger服务的启动过程分析
  2. iOS原生地图与高德地图的使用
  3. const深度总结(effective C++)
  4. 技术干货 | jsAPI 方式下的导航栏的动态化修改
  5. SAP Fiori Elements 应用的 i18n 语法使用方式
  6. gcc -E 产生预编译后的文件
  7. 守护(后台)进程的创建
  8. 高晓松侃5G!2019开年大讲揭示运营商的秘密
  9. 致远互联开始向云端生态发力奔跑
  10. imp执行错误IMP-00010
  11. Go语言反射(reflect)
  12. 为什么我朋友的移动手机打不开我的网站却能打开www.ip138.com?
  13. 公众号开发素材管理效果演示-微信开发素材管理1
  14. 小红书运营模式是怎样的?弄清楚小红书底层逻辑
  15. PHP取整,四舍五入取整、向上取整、向下取整、小数截取
  16. 一心多用多线程-线程池ThreadPoolExecutor-看这篇就够了
  17. MFC中使用sqlite3操作数据库 创建,插入数据,查询数据
  18. 你的程序员女孩「GitHub 热点速览 v.22.09」
  19. centos8 安装docker
  20. 2020美赛数学建模 校级培训——Week_3

热门文章

  1. Genymotion unfortunately has stopped
  2. linux echo 字体大小 背景 字体颜色 的编码
  3. PCIe接口二,三事
  4. 漫画 | 如何向外行解释,Bug是如何产生的?
  5. input lable 事件
  6. 基于Android的地铁查询系统app
  7. 超融合一体机如何影响私有云部署
  8. 华硕ddns注册ip地址不正确_华硕路由器 ddns注册成功,但是连接不上,怎么修改成别的ddns...
  9. OpenGL学习记录
  10. 【Leetcode 3】无重复字符的最长子串