我正在学习matplotlib脚本以显示下一个情节,如果我点击下一个'按钮。
数据是这样的:
# data.txt
10 30 21 22
12 36 22 23
13 37 23 24
14 38 24 25
15 39 25 26
16 40 26 27
要绘制的代码是:
#!python
# -*- coding: utf-8 -*-#
# Imports
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
data = np.genfromtxt('data.txt')
idx = [1,2,3]
fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.2)
x = data[:,0]
y = data[:,2]
l, = plt.plot(x, y, lw=2)
colors = ['r','g','b']
linestyles = ['-','--',':']
class Index(object):
ind = 0
def next(self, event):
i = self.ind % len(idx)
ydata = data[:,idx[i]]
l.set_ydata(ydata)
l.set_color(colors[i])
l.set_linestyle(linestyles[i])
self.ind += 1
plt.title('Plot ' + str(i))
plt.draw()
def prev(self, event):
i = self.ind % len(idx)
ydata = ydata = data[:,idx[i]]
l.set_ydata(ydata)
self.ind -= 1
plt.draw()
callback = Index()
axprev = plt.axes([0.7, 0.05, 0.1, 0.075])
axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
bnext = Button(axnext, 'Next')
bnext.on_clicked(callback.next)
bprev = Button(axprev, 'Previous')
bprev.on_clicked(callback.prev)
plt.show()
这里的代码适用于两个图,但是,其中一个图是空的。 y轴限制是固定的。
尝试
我试过了l.set_ylim
然而它失败了。
问题
a)我们如何在这个例子中显示所有的情节?
b)如何在图的顶部显示标题?它显示在底部
右上角。
相关链接:
https://matplotlib.org/examples/widgets/buttons.html
https://matplotlib.org/api/_as_gen/matplotlib.lines.Line2D.html
答案 0 :(得分:1)
a)我们如何在这个例子中显示所有图?
设置绘图的限制,以使数据在这些限制范围内ax.set_ylim(19,41)
。
b)如何在图的顶部显示标题?
在绘图中创建新轴后,请勿使用plt.title
。而是使用ax.set_title
。
完整的工作示例:
u = u"""10 30 21 22
12 36 22 23
13 37 23 24
14 38 24 25
15 39 25 26
16 40 26 27"""
import io
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
data = np.genfromtxt(io.StringIO(u))
idx = [1,2,3]
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.set_ylim(19,41)
x = data[:,0]
y = data[:,1]
l, = ax.plot(x, y, lw=2)
colors = ['r','g','b']
linestyles = ['-','--',':']
class Index(object):
ind = 0
def next(self, event):
self.ind += 1
self.plot()
def prev(self, event):
self.ind -= 1
self.plot()
def plot(self):
i = self.ind % len(idx)
ydata = data[:,idx[i]]
l.set_ydata(ydata)
l.set_color(colors[i])
l.set_linestyle(linestyles[i])
ax.set_title('Plot ' + str(i))
fig.canvas.draw_idle()
callback = Index()
axprev = plt.axes([0.7, 0.05, 0.1, 0.075])
axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
bnext = Button(axnext, 'Next')
bnext.on_clicked(callback.next)
bprev = Button(axprev, 'Previous')
bprev.on_clicked(callback.prev)
plt.show()
答案 1 :(得分:1)
以下是基于上述建议的更新答案。
#!python
# -*- coding: utf-8 -*-#
"""
Click next to see next plot.
"""
# Imports
import io
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
def click_next(data):
# plot variables
plot_nums = np.arange(1, data.shape[1])
colors = list('rgbcmyk')*10
linestyles = ['-','--','-.', ':', '-','--','-.']*10
class Index(object):
ind = 0
def next(self, event):
self.plot()
self.ind += 1
def prev(self, event):
self.plot()
self.ind -= 1
def plot(self):
i = self.ind % len(plot_nums)
ydata = data[:,plot_nums[i]]
l.set_ydata(ydata)
l.set_color(colors[i])
l.set_linestyle(linestyles[i])
ax.set_title('Plot ' + str(i+1))
fig.canvas.draw_idle()
# Now plot
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
# Get y limits
ydat = data[:,1:]
ymin = np.min(ydat,axis=0).min() -1
ymax = np.max(ydat,axis=0).max() +1
ax.set_ylim(ymin,ymax)
print(ymin,ymax)
# Plot first plot
x = data[:,0]
y = data[:,1]
ax.set_title('Plot 1')
l, = ax.plot(x, y, lw=2,c='r')
# matplotlib Button
callback = Index()
axprev = plt.axes([0.7, 0.05, 0.1, 0.075])
axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
bnext = Button(axnext, 'Next')
bnext.on_clicked(callback.next)
bprev = Button(axprev, 'Previous')
bprev.on_clicked(callback.prev)
# show the plot
plt.show()
def main():
"""Run main function."""
u = u"""
10 10 20 30 40 50
12 12 22 36 42 51
13 13 23 37 43 52
14 14 24 38 44 53
15 15 25 39 45 54
16 16 26 40 46 55"""
data = np.genfromtxt(io.StringIO(u))
click_next(data)
if __name__ == "__main__":
main()