首页 > 解决方案 > 如何在绘图内放大

问题描述

该图显示了模拟神经元的电压曲线:

情节 1

我想在右上角放置一个放大图,以便您可以更好地查看线条的当前波动(这里的比例是您几乎看不到它们)

附上情节的代码

定义电压幅度高度的因子
v_amp_factor = 1/(50)

##### distances between lines and x-axis
offset = np.cumsum(distance_comps_middle)/meter
offset = (offset/max(offset))*10
plt.close(plot_name)
voltage_course = plt.figure(plot_name)
for ii in comps_to_plot:
    plt.plot(time_vector/ms, offset[ii] - v_amp_factor*(voltage_matrix[ii, :]-V_res)/mV, "#000000")
plt.yticks(np.linspace(0,10, int(length_neuron/mm)+1),range(0,int(length_neuron/mm)+1,1))
plt.xlabel('Time/ms', fontsize=16)
plt.gca().invert_yaxis() # inverts y-axis => - v_amp_factor*(.... has to be written above

##### no grid
plt.grid(False)
plt.savefig('plot_name', dpi=600)
plt.show(plot_name)
parameter description: 
   Parameters
    ----------
    plot_name : string
        This defines how the plot window will be named.
    time_vector : list of time values
        Vector contains the time points, that correspond to the voltage values
        of the voltage matrix.
    voltage_matrix : matrix of membrane potentials
        Matrix has one row for each compartment and one columns for each time
        step. Number of columns has to be the same as the length of the time vector
    comps_to_plot : vector of integers
        This vector includes the numbers of the compartments, which should be part of the plot.
    distance_comps_middle : list of lengths
        This list contains the distances of the compartments, which allows that
        the lines in the plot are spaced according to the real compartment distances
    length_neuron : length
        Defines the total length of the neuron.
    V_res : voltage
        Defines the resting potential of the model.

标签: pythonmatplotlib

解决方案


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

# let's plot something similar to your stuff
t = np.linspace(-5, 5, 2001)
y = np.exp(-20*t**2)
fig, ax = plt.subplots()
for i in range(14):
    start = 900-10*i
    ax.plot(t[1000:1500], -5*y[start:start+500]/(1+i*0.3)+i, 'k')
ax.set_ylim((15, -10)) ; ax.set_yticks(range(14))

# now, create an inset axis, in the upper right corner, with
# a zoom factor of two
zax = zoomed_inset_axes(ax, 2, loc=1)

# plot again (PLOT AGAIN) the same stuff as before in the new axes
for i in range(14):
    start = 900-10*i
    zax.plot(t[1000:1500], -5*y[start:start+500]/(1+i*0.3)+i, 'k')

# and eventually specify the x, y limits for the zoomed plot,
# as well as the tick positions
zax.set_xlim((0.2, 0.7)) ; zax.set_xticks((0.2, 0.3, 0.4, 0.5, 0.6, 0.7))
zax.set_ylim((1, -6)) ; zax.set_yticks([1]+[-i*0.5 for i in range(12)]) ;

# that's all folks
plt.show()

我的结果


推荐阅读