Fig.gca () vs. fig.add_subplot ()

Most examples of object oriented matplotlib get an Axis object with something like

import matplotlib.pyplot as plt fig1 = plt.figure() ax1 = fig1.add_subplot(111) ax1.plot(...... etc. 

Which I always considered unobvious, especially from the point of view of matlab. I recently discovered that equivalent results can be obtained using

 ax1 = fig1.gca() # "GetCurrentAxis" 

This makes me more intelligent (perhaps only because of the use of Matlab in the past). Why add_subplot () with a confusing 111 argument selected as the preferred way to get the axis object? Is there any functional difference?

Thanks!

+16
source share
2 answers

plt.gca gets the current axis, creating if necessary. This is equivalent only for the simplest 1 axes.

The preferred way is to use plt.subplots (and the documents / examples are really a bit behind if you want to start contributing, updating the documents is a great place to start):

 fig, ax = plt.subplots(1, 1) 

or

 fig, (ax1, ax2) = plt.subplots(2, 1) 

etc.

+19
source

There are three ways to create a 3D instance:

 plt.gca(projection='3d') plt.subplot(projection='3d') fig = plt.figure() fig.add_subplot(111, projection='3d') 

Maybe the third way is more complicated.

0
source

Source: https://habr.com/ru/post/978918/


All Articles