python - 如何在绘图内放大
问题描述
该图显示了模拟神经元的电压曲线:
我想在右上角放置一个放大图,以便您可以更好地查看线条的当前波动(这里的比例是您几乎看不到它们)
附上情节的代码
定义电压幅度高度的因子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.
解决方案
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()
推荐阅读
- reactjs - 多个 combineReducer 后 PreloadedState 未正确反映
- asp.net - 如何在 WebApi 2 ASP.NET 中对用户进行身份验证?
- ruby-on-rails - 如何将通过 cron 任务调用的 Rails Rake 任务输出移植到 Rails 生产服务器输出?
- protractor - Angular 8 - ng e2e 不适用于默认的 chrome 驱动程序
- raspberry-pi3 - 无法从 EM18 + Raspberrypi 持续读取 RFID 卡
- laravel - Laravel 模型工厂 make() 在外键对象创建中绕过 create()
- vba - 使用VBA从excel粘贴后如何在Powerpoint中重命名形状
- azure - 如何更改 Azure CDN 上的 MIME 类型
- performance-testing - 如何使用空手道向 Galting 测试添加断言?
- c++ - 将数据从 std::Vector 存储到 Eigen::Vector 时出错