问题
I have subplots in an ipython notebook. I can zoom in inline using the mpld3
module. However, right now I can only zoom into a rectangle. Due to my application's nature I need horizontal zoom.
Is there a way to zoom horizontally (using your cursor) in matplotlib? Better yet can I set the zoom to be horizontal via code without any keyboard manipulation? In Matlab, I can do this by setting:
figure(1); h=zoom; set(h,'Motion','horizontal','Enable','on');
Here is a minimal example in python:
%matplotlib inline
import matplotlib.pyplot as plt
import mpld3
import numpy as np
mpld3.enable_notebook()
x = np.arange(100)
y = np.sin(x)
z = np.cos(x)
ax1 = plt.subplot(2,1,1)
ax1.plot(x,y)
ax2 = plt.subplot(2,1,2, sharex=ax1)
ax2.plot(x,z)
回答1:
My suggestion would be to use Plotly, which is great to create interactive plots in Jupyter.
You can get more info on how to plot time series with it HERE and info on subplots HERE. Here is what I made with your data, hopefully it does what you want!
%matplotlib inline
import plotly
from plotly import tools
import numpy as np
plotly.offline.init_notebook_mode() # run at the start of every notebook
x = np.arange(100)
y = np.sin(x)
z = np.cos(x)
trace1 = go.Scatter(x=x, y=y)
trace2 = go.Scatter(x=x, y=z)
fig = tools.make_subplots(rows=2, cols=1)
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 2, 1)
fig['layout'].update(height=600, width=600)
plotly.offline.iplot(fig)
来源:https://stackoverflow.com/questions/36085978/ipython-notebook-horizontal-zoom