我以@DanHickstein的答案为基础,涵盖了plot、scatter和axhline/axvline缩放x轴或y轴的情况。它可以简单地称为autoscale()来处理最新的轴。如果你想编辑它,请fork it on gist。def autoscale(ax=None, axis='y', margin=0.1):

'''Autoscales the x or y axis of a given matplotlib ax object

to fit the margins set by manually limits of the other axis,

with margins in fraction of the width of the plot

Defaults to current axes object if not specified.

'''

import matplotlib.pyplot as plt

import numpy as np

if ax is None:

ax = plt.gca()

newlow, newhigh = np.inf, -np.inf

for artist in ax.collections + ax.lines:

x,y = get_xy(artist)

if axis == 'y':

setlim = ax.set_ylim

lim = ax.get_xlim()

fixed, dependent = x, y

else:

setlim = ax.set_xlim

lim = ax.get_ylim()

fixed, dependent = y, x

low, high = calculate_new_limit(fixed, dependent, lim)

newlow = low if low < newlow else newlow

newhigh = high if high > newhigh else newhigh

margin = margin*(newhigh - newlow)

setlim(newlow-margin, newhigh+margin)

def calculate_new_limit(fixed, dependent, limit):

'''Calculates the min/max of the dependent axis given

a fixed axis with limits

'''

if len(fixed) > 2:

mask = (fixed>limit[0]) & (fixed < limit[1])

window = dependent[mask]

low, high = window.min(), window.max()

else:

low = dependent[0]

high = dependent[-1]

if low == 0.0 and high == 1.0:

# This is a axhline in the autoscale direction

low = np.inf

high = -np.inf

return low, high

def get_xy(artist):

'''Gets the xy coordinates of a given artist

'''

if "Collection" in str(artist):

x, y = artist.get_offsets().T

elif "Line" in str(artist):

x, y = artist.get_xdata(), artist.get_ydata()

else:

raise ValueError("This type of object isn't implemented yet")

return x, y

它和它的前身一样,有点老套,但这是必要的,因为集合和行有不同的返回xy坐标的方法,而且由于axhline/axvline只有两个数据点,所以很难使用。

这就是它的作用:fig, axes = plt.subplots(ncols = 4, figsize=(12,3))

(ax1, ax2, ax3, ax4) = axes

x = np.linspace(0,100,300)

noise = np.random.normal(scale=0.1, size=x.shape)

y = 2*x + 3 + noise

for ax in axes:

ax.plot(x, y)

ax.scatter(x,y, color='red')

ax.axhline(50., ls='--', color='green')

for ax in axes[1:]:

ax.set_xlim(20,21)

ax.set_ylim(40,45)

autoscale(ax3, 'y', margin=0.1)

autoscale(ax4, 'x', margin=0.1)

ax1.set_title('Raw data')

ax2.set_title('Specificed limits')

ax3.set_title('Autoscale y')

ax4.set_title('Autoscale x')

plt.tight_layout()

python plot坐标轴显示比例一致,Matplotlib-固定x轴比例和自动缩放y轴相关推荐

  1. python plot坐标轴显示比例一致_绘图,x轴和y轴的比例相同

    我正在为python使用plotly,无法设置x和y轴,以便它们具有相同的比例: 这是我的布局:layout = Layout( xaxis=XAxis( range=[-150, 150], sho ...

  2. python调整屏幕缩放比例_Matplotlib-固定x轴缩放比例和自动缩放y轴 - python

    我只想绘制部分数组,固定x部分,但让y部分自动缩放.我尝试如下所示,但是它不起作用. 有什么建议么? import numpy as np import matplotlib.pyplot as pl ...

  3. python使用matplotlib可视化、使用locator_params函数自定义调整Y轴坐标轴的刻度的个数(customize number of ticks of y axis)

    python使用matplotlib可视化.使用locator_params函数自定义调整Y轴坐标轴的刻度的个数(customize number of ticks of y axis) 目录

  4. 坐标轴刻度取值算法-基于魔数数组-源于echarts的y轴刻度计算需求

    数值型坐标轴刻度计算算法 前言 算法描述 上代码 代码运行效果 ts版本(2021/3/10补充) 结语 前言 因实习的公司是做大数据的,而我的工作刚好又是需要绘制一些数据图表的.绘制图表有许多现成的 ...

  5. python plot 坐标轴范围,Python,Matplotlib,子图:如何设置轴范围?

    How can I set the y axis range of the second subplot to e.g. [0,1000] ? The FFT plot of my data (a c ...

  6. python plot 坐标轴范围_Python-Matplotlib 15 坐标轴范围

    原博文 2017-02-22 10:52 − Python-Matplotlib 15 坐标轴范围 EG1 import numpy as np import matplotlib.pyplot as ...

  7. python matplotlib.pyplot.xticks() yticks() (设置x或y轴对应显示的标签)

    from matplotlib\pyplot.py def xticks(ticks=None, labels=None, **kwargs):"""Get or set ...

  8. python plot 图像大小_设置matplotlib图像的figsize和dpi

    我们可以通过两个参数来控制matplotlib图像的大小,在创建figure对象的时候,有figsize和dpi. figsize是一个tuple,用来指定width和height的inch: dpi ...

  9. python 图 鼠标 显示_在matplotlib图形窗口(带imshow)中,如何删除、隐藏或重新定义鼠标的显示位置?...

    是的,你可以.但比你想象的要难. 您看到的鼠标跟踪标签是根据鼠标跟踪调用matplotlib.axes.axes.format_coord生成的.您必须创建自己的Axes类(重写format_coor ...

最新文章

  1. C# 操作Cookie类
  2. 操作系统第三章-内存管理
  3. flink check-point save-point理解
  4. 关于活动对像要注意的问题
  5. MachineLearning(4)-核函数与再生核希尔伯特空间
  6. 同济大学Python程序设计基础 实验一:Python程序设计基础
  7. Python:体脂计算
  8. win10系统迁移后系统重装_Win10如何把系统迁移到固态硬盘上?Win10系统迁移到SSD上教程...
  9. html中怎样写渐变色代码,html颜色渐变代码 怎样用css实现网页背景颜色渐变
  10. BUUCTF[强网杯 2019]随便注 的三种解法
  11. 【数据结构与算法】之深入解析“香槟塔”的求解思路与算法示例
  12. JS jQuery 赋值取值整理
  13. C#模仿腾讯QQ源码下载(附效果图)_张童瑶的博客
  14. 神经网络初探(BP 算法、手写数字识别)
  15. 次世代游戏建模学习,带你轻松认识并掌握3Dmax
  16. 数字时代的新一代数据安全
  17. 敬往事一杯酒,敬已逝去的 2018~
  18. 波士顿科学宣布收购Baylis医疗;启明医疗协议收购诺诚医疗;百济神州宣布百悦泽获批用于治疗套细胞淋巴瘤 | 医药健闻...
  19. 【学习笔记】Java虚拟机(二)内存区域与内存溢出
  20. 生产mysql遇到kill不掉的sql的解决方法

热门文章

  1. vim删除奇数行_如何用vim删除特定的偶数行?
  2. 【VScode】搜索栏语法
  3. 数商云:以数字化全面促进传统产业升级,纺织行业最大限度实现精益管理
  4. MySQL数据库,数据的约束
  5. 数据分析师1.4量化交易策略的回测方法和技巧
  6. LeetCode 1061. Lexicographically Smallest Equivalent String
  7. 【产品经理】产品经理的日常工作
  8. [CTF]中那些脑洞大开的编码和加密
  9. C++程序设计实践里面石头剪刀布版王者农药实例
  10. E01 -- CSS样式中 报错 { expectedcss(css-lcurlyexpected)