使用set_xlim()对图进行切片后,matplotlib自动缩放(axis ='y') [英] matplotlib autoscale(axis='y') after slicing plot with set_xlim()

查看:123
本文介绍了使用set_xlim()对图进行切片后,matplotlib自动缩放(axis ='y')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为演示,我正在绘制x ^ 0到x ^ 9,其中x值的范围是10到20.

As a demonstration, I'm plotting x^0 through x^9 with x values in range from 10 to 20.

然后我将这些图像切成薄片,以便有9个切片:

Then I'm slicing those images so that I have 9 slices:

x =(10至11),(11至12)等到(18至19)

x = (10 to 11), (11 to 12) etc. to (18 to 19)

我希望裁剪图像,以便每个切片中的y值始终从上到下分布,但是我得到的是自动缩放始终缩放到整个数据集而不是当前切片.

I want my images cropped so that y values are always spread from top to bottom in each slice, but what I'm getting is autoscale always scales to the full dataset rather than the current slice.

import matplotlib.pyplot as plt
import numpy as np


# create some test data
for i in range(10):
    x = np.arange(10,20)
    y = x**i
    plt.plot(x,y,c='red',marker='.',ms=2)

# get all x values in chart and reduce to a sorted list set
xd = [] 
for n in range(len(plt.gca().get_lines())):
        line = plt.gca().get_lines()[n]
        xd.append((line.get_xdata()).tolist())
xd = [item for sublist in xd for item in sublist]
xd = sorted(list(set(xd)))

# attempt to plot slices of x with autoscaled y
ax = plt.gca()
for i in range(len(xd)-1):
    ax.set_xlim([xd[i],xd[i+1]])
    ax.axes.autoscale(enable=True,axis='y', tight=True)
    plt.pause(1) #timing
    #uncommenting the next line will create nine tiny (6kb) image files
    #plt.savefig(('image_%s.png' % i), bbox_inches=0, dpi=48)

在我的实际应用程序中,我试图以此方式从随机数据中生成10万个微型图像作为数据库.对于每个x,都有2到200个y值.然后,我使用OpenCV对历史数据库中的新图像进行图像匹配,以使其最适合.

In my actual application, I'm attempting to generate 100k tiny images in this manner as a database from stochastic data. For every x there are between 2 and 200 y values. Then I'm using OpenCV to image match new images to the best fit amongst the historical database.

对于OpenCV来说,至关重要的是y值应在每个图像中从上到下拉伸以找到良好的匹配.

Its critical that the y values are stretched from top to bottom in each image for OpenCV to find a good match.

如果有帮助,我的x值将始终为int()类型且等距

if it helps my x values will always be int() type and equally spaced

ETA:我试图在此处实施某些解决方案,但没有取得任何进展:

ETA: I've attempted to implement some of the solutions here but have made no progress:

Matplotlib-修复x轴刻度和y轴自动刻度

Matplotlib根据手动缩放的x轴缩放y轴

但至少我学到了:

自动缩放始终使用整个数据范围,因此y轴为 根据y数据的全部范围进行缩放,而不仅仅是 x限制.

Autoscaling always uses the full range of the data, so the y-axis is scaled by full extent of the y-data, not just what's within the x-limits.

在这里仍然没有解决方案

still no solution that works here though

def autoscale_y()

由@DanHickstein提出

presented by @DanHickstein

给我:

h = np.max(y_displayed) - np.min(y_displayed)
ValueError: zero-size array to reduction operation maximum which has no identity

从这些链接中,我不确定在我的for循环中在何处实现@Joe Kington的遮罩解决方案.

From those links, I'm unsure where to implement @Joe Kington's mask solution in my for loops.

我现在正在使用此处建议的@bernie解决方案,以在给定X的情况下获取Y值:

I'm now working with @bernie solution proposed here to get Y values given X:

如何从图形中提取点?

然后我可以手动设置X的最小和最大Y值来设置set_ylim()吗?

maybe then I can set_ylim() given the min and max Y values at that X manually?

如果有一种方法可以在定义的xlim中自动缩放为标准的matplotlib方法,则这样会容易得多

This would be so much easier if there was a way to autoscale within the defined xlim as a standard matplotlib method

推荐答案

我昨晚通过创建一个以x为键,y分别为值列表的字典解决了我的问题.

I solved my issue last night by creating a dictionary with x's as keys and a respective list of y's as values.

这是由于数据是由函数y = x ** i

This occurs as the data is created by the function y=x**i

实质上,我正在创建字典结构伪代码:

in essence I'm creating dictionary structure pseudocode:

data[x0] = [x0y1,x0y2,x0y3....]
data[x1] = [x1y1,x1y2,x1y3....]
data[x2] = [x2y1,x2y2,x2y3....]
etc.

我以后可以引用任何给定x的所有y值.从那里,找到切片的左侧和右侧的最小和最大y值,以手动设置ylim.如果您的xlim切片宽度超过一个x段,则必须对xlim中的每个x切片重复该过程.在我的实例中,我的x切片恰好是一个分段的宽度.

I can later reference all the y values at any given x. From there, find the min and max y value for the left and right side of my slice to manually set ylim. If your xlim slice was more than one x segment wide you'd have to repeat the process for each respective x slice within your xlim. In my instance, my x slices are exactly one segment wide.

import matplotlib.pyplot as plt
import numpy as np

# move my range function out of my data creation loop
x = np.arange(10,20,1)

# create a dictionary of my data with x values as keys
data = {}
for i in range(len(x)):
   data[x[i]]=[]

# create some test data
for i in range(10):
    y = x**i
    plt.plot(x,y,c='red',marker='.',ms=2)

    # store my y data to my data dictionary as its created
    xx = x[-len(y):]
    for j in range(len(xx)):
        data[xx[j]].append(y[j])

# get all x values in chart and reduce to a sorted list set
xd = [] 
for n in range(len(plt.gca().get_lines())):
        line = plt.gca().get_lines()[n]
        xd.append((line.get_xdata()).tolist())
xd = [item for sublist in xd for item in sublist]
xd = sorted(list(set(xd)))

# attempt to plot slices of x with autoscaled y
ax = plt.gca()
for i in range(len(xd)-1):
    ax.set_xlim([xd[i],xd[i+1]])

    # reference my min and max y values by left and right borders of x slice
    ymin = min(min(data[xd[i]]), min(data[xd[i+1]]))
    ymax = max(max(data[xd[i]]), max(data[xd[i+1]]))
    # manually set y limits
    ax.set_ylim([ymin,ymax])

    #eliminate my autoscale call
    #ax.axes.autoscale(enable=True,axis='y', tight=True)
    plt.pause(1) #timing

现在绘制时,y会自动缩放到x切片,而不是整个数据集.

Now when it plots, y is autoscaled to the x slice, not the entire dataset.

这篇关于使用set_xlim()对图进行切片后,matplotlib自动缩放(axis ='y')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆