The plot for specific axes in Matplotlib

How can I display specific axes in matplotlib? I created my own object, which has its own construction method and accepts standard arguments and kwargs to adjust the line color, width, etc. I would also like to be able to draw certain axes.

I see that there is an axes property that accepts an Axes object, but regardless of what it still only draws to the last created axes.

Here is an example of what I want

fig, ax = subplots(2, 1) s = my_object() t = my_object() s.plot(axes=ax[0]) t.plot(axes=ax[1]) 
+4
source share
2 answers

As I said in a comment, read How do I associate the pyplot function with a shape instance? to explain the difference between OO and state-machine interfaces with matplotlib .

You have to change your build functions to be something like

 def plot(..., ax=None, **kwargs): if ax is None: ax = gca() ax.plot(..., **kwargs) 
+5
source

You can use the graph function of certain axes:

 import matplotlib.pyplot as plt from scipy import sin, cos f, ax = plt.subplots(2,1) x = [1,2,3,4,5,6,7,8,9] y1 = sin(x) y2 = cos(x) plt.sca(ax[0]) plt.plot(x,y1) plt.sca(ax[1]) plt.plot(x,y2) plt.show() 

This should appear in two different subheadings.

+1
source

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


All Articles