如何通过 Matplolib 缩放绘图的一部分 [英] How to zoom a part of plot by Matplolib

查看:39
本文介绍了如何通过 Matplolib 缩放绘图的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想放大,例如,情节的中心部分,但我有一些问题.我想做这样的事情,但只用简单的一行即可.我怎样才能做到这一点?我有这样的一段代码:

 将matplotlib.pyplot导入为plt进口泡菜从 mpl_toolkits.axes_grid1.inset_locator 导入 zoomed_inset_axes从 mpl_toolkits.axes_grid1.inset_locator 导入 mark_inset将numpy导入为np无花果,ax = plt.subplots(figsize = [5,4])范围 = (0, 100, 0, 50)xx = np.linspace(0,100,1000)Z2 = [np.sin(x) for x in xx]ax.imshow(Z2,范围=范围,插值=最近",原点=下")axins = zoomed_inset_axes(ax, 6, loc=1) # zoom = 6mark_inset(ax,axins,loc1 = 2,loc2 = 4,fc ="none",ec ="0.5")plt.draw()plt.show()

Python说:

 /usr/bin/python2.7"/home/kenenbek/Documents/Pycharm项目/PycharmProjects/Multi-Agent/vizualization.py"/usr/local/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib 正在使用 fc-list 构建字体缓存.这可能需要一点时间.warnings.warn("Matplotlib正在使用fc-list构建字体缓存.这可能需要一些时间.")回溯(最近一次调用最后一次):文件/home/kenenbek/Documents/Pycharm Projects/PycharmProjects/Multi-Agent/vizualization.py",第55行,在<module>原点=下")文件/usr/local/lib/python2.7/dist-packages/matplotlib/__init__.py",第1812行,在内部返回 func(ax, *args, **kwargs)文件/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_axes.py",第 4947 行,在 imshow 中im.set_data(X)set_data中的文件"/usr/local/lib/python2.7/dist-packages/matplotlib/image.py",行449raise TypeError("图像数据无法转换为浮点数")TypeError:图像数据无法转换为浮点型

解决方案

是的,您可以使用非图像作图.

I want to zoom, for example, a central part of plot, but I have some problem. I want to do something like that, but only with a simple line. How can I do that? I have such piece of code:

import matplotlib.pyplot as plt
import pickle
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
import numpy as np

fig, ax = plt.subplots(figsize=[5,4])
extent = (0, 100, 0, 50)
xx = np.linspace(0, 100, 1000)
Z2 = [np.sin(x) for x in xx]
ax.imshow(Z2, extent=extent, interpolation="nearest",
          origin="lower")
axins = zoomed_inset_axes(ax, 6, loc=1) # zoom = 6
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")

plt.draw()
plt.show()

Python says:

/usr/bin/python2.7 "/home/kenenbek/Documents/Pycharm Projects/PycharmProjects/Multi-Agent/vizualization.py"
/usr/local/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
Traceback (most recent call last):
  File "/home/kenenbek/Documents/Pycharm Projects/PycharmProjects/Multi-Agent/vizualization.py", line 55, in <module>
    origin="lower")
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/__init__.py", line 1812, in inner
    return func(ax, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_axes.py", line 4947, in imshow
    im.set_data(X)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/image.py", line 449, in set_data
    raise TypeError("Image data can not convert to float")
TypeError: Image data can not convert to float

解决方案

Yes, you can do it with a plot that is not an image. Check this post for an example.

In your case, I think that the problem is trying to plot a list as an image.

Check this code for an example that works for imshow or countourf (the one used here).

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
import numpy as np

fig, ax = plt.subplots(figsize=(5,4))
extent = (0, 100, 0, 50)
x, y = np.mgrid[-5:5:100j, -5:5:100j]
z = np.sin(2*x)*np.sin(y**2)
ax.contourf(x, y, z, cmap="YlGnBu_r")
axins = zoomed_inset_axes(ax, 2, loc=1)
axins.contourf(x, y, z, cmap="YlGnBu_r")
axins.set_xlim(1, 2)
axins.set_ylim(1, 2)
plt.xticks(visible=False)
plt.yticks(visible=False)
mark_inset(ax, axins, loc1=2, loc2=4, fc="none", ec="0.5")
plt.draw()
plt.show()

这篇关于如何通过 Matplolib 缩放绘图的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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