我想缩放,例如,情节的中心部分,但我有一些问题。 我想做那样的事情,但只是用一条简单的线条。我怎样才能做到这一点? 我有这样的代码:
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说:
/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
答案 0 :(得分:1)
是的,你可以使用不是图像的情节来做到这一点。 Check this post举个例子。
在您的情况下,我认为问题是尝试将列表绘制为图像。
请检查此代码,查看适用于imshow
或countourf
的示例(此处使用的示例)。
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()
答案 1 :(得分:1)
我不确定您是否要绘制图像或轮廓。但是,这是ndarray
类型数据的版本。通过执行image = numpy.genfromtxt(fname=file)
,您可以轻松地将任何仅具有单通道(亮度)值的文本图像转换为ndarray。我的代码很快来自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()