CB95
CB95

Reputation: 77

How to zoom inside a plot

this plot shows the voltage course lines of a simulated neuron:

plot 1

i would like to place a zoom in plot in the upper right corner so that you can see the current fluctuations of the lines better (the scale here is such that you can hardly see them)

attached the code for the plot

factor to define voltage-amplitude heights
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.

Upvotes: 2

Views: 5172

Answers (2)

gboffi
gboffi

Reputation: 25023

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

my result

Upvotes: 2

Zulu
Zulu

Reputation: 21

Doing some research I found a method that should be exactly what you are looking for using the inset_axes method of matplotlib, you can find a nice working example in the docs. I hope it helps, I would have tried to apply it to your code, but without the data you used to plot there wasn't much I could do.

As you can find in the docs this method will allow you to do something as shown in this image:

enter image description here

Upvotes: 0

Related Questions