Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Tuple Unpacking with graphs in matplotlib
#1
I hope you are all having a good day. I am currently taking a MOOC on Python. We went over a bit of code in matplotlib:

Fig=plt.figure()
Fig,axes=plt.subplots(nrows=1,ncols=2)

The instructor said that "fig, axes" is a way of doing tuple unpacking. I know what tuple unpacking is, however, I don't understand it in this context. If someone could please explain it would be greatly appreciated.
Quote
#2
plt.subplots() returns tuple containg figure object and arrray of axis objects (if atleast one of nrows or ncols is > 1).

When you want plot with two subplots in one row, you use fig, axes = plt.subplots(1,2), unpacking assigns figure into fig and axis array into axes. After that you can use axes to access specified subplots, while fig is used to control entire figure. Simple example:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-2, 2, 400)
fig, axes = plt.subplots(1,2)
axes[0].plot(x, x**2)
axes[1].plot(x, x**3)
fig.savefig('boo.png', dpi=200)
[Image: OOKQKp8.png]
Quote
#3
(Mar-23-2017, 04:51 PM)zivoni Wrote: plt.subplots() returns tuple containg figure object and arrray of axis objects (if atleast one of nrows or ncols is > 1). When you want plot with two subplots in one row, you use fig, axes = plt.subplots(1,2), unpacking assigns figure into fig and axis array into axes. After that you can use axes to access specified subplots, while fig is used to control entire figure. Simple example:
 import numpy as np import matplotlib.pyplot as plt x = np.linspace(-2, 2, 400) fig, axes = plt.subplots(1,2) axes[0].plot(x, x**2) axes[1].plot(x, x**3) fig.savefig('boo.png', dpi=200) 
[Image: OOKQKp8.png]

That was exceedingly helpful. Thank you!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Spacing pie chart colours evenly in matplotlib? Giovanni_diJacopo 1 292 Jul-12-2019, 12:31 PM
Last Post: scidam
  Unable to identify Tuple index error appmkl 4 240 Jun-28-2019, 10:12 AM
Last Post: appmkl
  Keras + Matplotlib causing crash spearced 2 370 May-17-2019, 09:47 AM
Last Post: spearced
  MatPlotLib 2d plot of current density? ruben 0 173 May-13-2019, 06:47 AM
Last Post: ruben
  How to arrange the four pictures of a matplotlib.pyplot? vokoyo 0 323 Apr-04-2019, 10:58 PM
Last Post: vokoyo
  plotting of graphs mudezda1 2 377 Feb-11-2019, 12:44 PM
Last Post: mudezda1
  Numpy and matplotlib in incorporated Python. CSKir 7 1,080 Jan-15-2019, 10:44 AM
Last Post: CSKir
  Oct2py and Matplotlib installation problems Leloup 2 517 Nov-23-2018, 03:32 PM
Last Post: Leloup
  how can I create a recursive graphic with matplotlib royer14 3 557 Nov-22-2018, 05:00 PM
Last Post: Gribouillis
  how can I create graphics using matplotlib royer14 8 725 Nov-21-2018, 07:02 AM
Last Post: Gribouillis

Forum Jump:


Users browsing this thread: 1 Guest(s)