How to zoom a part of plot by Matplolib

旧街凉风 提交于 2020-06-17 15:57:09

问题


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

回答1:


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()




回答2:


I am not quite sure if you want to plot an image, or contours. However here is the version for ndarray type data. You can easily convert any text-image that has only single channel (luminance) values to an ndarray by doing image = numpy.genfromtxt(fname=file). My code was quickly derived from Basemap Inset Locators

    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
    from scipy import misc
    fig = plt.figure()
    ax = fig.add_subplot(111)

    image = misc.ascent()

    ax.imshow(image,extent=[0,512,0,512], aspect=(1))

    axins = zoomed_inset_axes(ax, 5, loc=1)
    axins.imshow(image, extent=[0,512,0,512],aspect=(1))
    axins.set_xlim(150, 200)
    axins.set_ylim(120, 170)
    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()


来源:https://stackoverflow.com/questions/34952752/how-to-zoom-a-part-of-plot-by-matplolib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!