2016-01-22 55 views
0

我想放大,例如,情節的中心部分,但我有一些問題。 我想做這樣的事情,但只能用簡單的線條。我怎樣才能做到這一點? enter image description here 我有這樣一段代碼:如何放大Matplolib的一部分繪圖

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 

回答

1

是的,你可以用一個情節是不是一個圖像做。 Check this post爲例。

就你而言,我認爲問題是試圖將列表作爲圖像繪製。

請查看此代碼,查看適用於imshowcountourf(此處使用的示例)的示例。

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

enter image description here

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