  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # matplotlib for loop to show, save and redraw all plots  » python » matplotlib for loop to show, save and redraw all plots

By : Tushar Sarma
Date : November 17 2020, 01:00 AM
it helps some times Here's my python code, , You should write your self a helper function: code :
``````def my_plotter(ax, t, z, est_z):
ln1 = ax.plot(t, z, 'bo-', label='sample data')
ln2 = ax.plot(t, est_z, 'go-', label='estimated sample data')
ax.xlabel('time')
ax.ylabel('data value')
ax.grid(True)
ax.legend()
return ln1 + ln2

for i in xrange(2):
# get the data
t = a[i+1:(i+1)*60, 2]
z = a[i+1:(i+1)*60, 3]
est_z = a[i+1:(i+1)*60, 6]
# make the figure
fig, ax = plt.subplots()
# do the plot
my_plotter(ax, t, z, est_Z)
# save
fig.savefig('test_{}.png'.format(i))
``````
``````# make one figure with 2 axes
fig, ax_lst = plt.subplots(1, 2)

for i, ax in zip(xrange(2), ax_lst):
# get the data
t = a[i+1:(i+1)*60, 2]
z = a[i+1:(i+1)*60, 3]
est_z = a[i+1:(i+1)*60, 6]
# do the plot
my_plotter(ax, t, z, est_Z)
# save the figure with both plots
fig.savefig('both.png')
`````` ## How to show matplotlib plots in python

By : user3913076
Date : March 29 2020, 07:55 AM
this one helps. I am sure the configuration of matplotlib for python is correct since I have used it to plot some figures. , In matplotlib you have two main options:
code :
``````import matplotlib.pyplot as plt

plt.plot(x, y)
plt.plot(z, t)
plt.show()
``````
``````import matplotlib.pyplot as plt
from matplotlib import interactive
interactive(True)

plt.plot(x, y)

plt.plot(z, t)
`````` ## Plots are not visible using matplotlib plt.show()

By : Baljit Singh
Date : March 29 2020, 07:55 AM
To fix this issue I'm really new in Python and Linux and I need help, I tried to use matplotlib for showing a simple plot in the following way: , As you can see:
code :
``````"The Gtk3Agg backend is known to not work on Python 3.x with pycairo."
``````
``````Try installing cairocffi.
``````
``````pip install cairocffi
``````
``````sudo apt-get install python3-dev
sudo apt-get install libffi-dev
`````` ## Matplotlib doesn't show plots on Mac; plt.show() hangs on 'MacOSX' backend

By : George Carey
Date : March 29 2020, 07:55 AM
will be helpful for those in need The answer, as pointed out by @ImportanceOfBeingErnest is that the backend configuration for me wasn't working. I'm not sure if I need to install some additional libraries or not. I decided not to use the MacOSX backend and used the Qt5Agg backend. This worked just fine and I didn't have to install any new libraries. ## Python: matplotlib - loop, clear and show different plots over the same figure

By : DAVINCII
Date : March 29 2020, 07:55 AM
With these it helps There are essentially two different ways to create animations in matplotlib
interactive mode
code :
``````import matplotlib.pyplot as plt

x = [1,1]
y = [1,2]

fig, (ax1,ax2) = plt.subplots(nrows=2, sharex=True, sharey=True)
line1, = ax1.plot(x)
line2, = ax2.plot(y)
ax1.set_xlim(-1,17)
ax1.set_ylim(-400,3000)
plt.ion()

for i in range(15):
x.append(x[-1]+x[-2])
line1.set_data(range(len(x)), x)
y.append(y[-1]+y[-2])
line2.set_data(range(len(y)), y)

plt.pause(0.1)

plt.ioff()
plt.show()
``````
``````import matplotlib.pyplot as plt
import matplotlib.animation

x = [1,1]
y = [1,2]

fig, (ax1,ax2) = plt.subplots(nrows=2, sharex=True, sharey=True)
line1, = ax1.plot(x)
line2, = ax2.plot(y)
ax1.set_xlim(-1,18)
ax1.set_ylim(-400,3000)

def update(i):
x.append(x[-1]+x[-2])
line1.set_data(range(len(x)), x)
y.append(y[-1]+y[-2])
line2.set_data(range(len(y)), y)

ani = matplotlib.animation.FuncAnimation(fig, update, frames=14, repeat=False)
plt.show()
``````
``````import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np

x = np.linspace(0,24*np.pi,512)
y = np.sin(x)

def fft(x):
fft = np.abs(np.fft.rfft(x))
return fft**2/(fft**2).max()

fig, (ax1,ax2) = plt.subplots(nrows=2)
line1, = ax1.plot(x,y)
line2, = ax2.plot(fft(y))
ax2.set_xlim(0,50)
ax2.set_ylim(0,1)

def update(i):
y = np.sin((i+1)/30.*x)
line1.set_data(x,y)
y2 = fft(y)
line2.set_data(range(len(y2)), y2)

ani = matplotlib.animation.FuncAnimation(fig, update, frames=60, repeat=True)
plt.show()
`````` ## How to effectively redraw multiple matplotlib plots with blit

By : user3047209
Date : March 29 2020, 07:55 AM
hope this fix your issue The problem is that the snapshot of the background is taken at a moment in time where the figure has not yet been shown on screen. At that point the figure is 10 by 10 inches large. Later, it is shown inside the QMainWindow and resized to fit into the widget.
Only once that has happened, it makes sense to take the background snapshot.
code :
``````import numpy as np
import sys

from matplotlib.backends.qt_compat import QtCore, QtWidgets
from matplotlib.figure import Figure

class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
layout = QtWidgets.QVBoxLayout(self._main)

self.static_canvas = FigureCanvas(Figure(figsize=(10, 10)))
axes = self.static_canvas.figure.subplots(2, 1)
self.ax1 = axes
self.ax2 = axes
self.ax1.cla()
self.ax2.cla()

button = QtWidgets.QPushButton('Click me!')
button.clicked.connect(self.update_canvas_blit)
# Fixing random state for reproducibility
np.random.seed(19680801)

# Create random data
N = 50000
x = np.random.rand(N)
y = np.random.rand(N)

self.ax1.scatter(x, y)
self.points = self.ax1.scatter([],[], s=5, color='red')

x = np.linspace(0, 1000, 100000)
self.ax2.plot(x, np.sin(x))
self.lines, = self.ax2.plot([],[], color='red')
self.static_canvas.draw()

self._later()

def _later(self, evt=None):
self.timer = self.static_canvas.new_timer(interval=1000)
self.timer.single_shot = True
self.timer.start()

def update_background(self, evt=None):
self.background1 = self.static_canvas.copy_from_bbox(self.ax1.bbox)
self.background2 = self.static_canvas.copy_from_bbox(self.ax2.bbox)

def update_canvas_blit(self):
N = 50
x = np.random.rand(N)
y = np.random.rand(N)

self.static_canvas.restore_region(self.background1)
self.points.set_offsets(np.c_[x,y])
self.ax1.draw_artist(self.points)
self.ax1.figure.canvas.blit(self.ax1.bbox)

self.static_canvas.restore_region(self.background2)
x = np.linspace(0, np.random.randint(500,1000), 1000)
self.lines.set_data(x, np.sin(x))
self.ax2.draw_artist(self.lines)
self.ax2.figure.canvas.blit(self.ax2.bbox)

if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = ApplicationWindow()
app.show()
qapp.exec_()
`````` 