How can I view an Axessubplot object in the same way as pyplot.show()?

Asked

Viewed 158 times

-1

I’m learning how to deal with pandas and after getting a Dataframe, the next step was to get a chart of this Dataframe "Upermarket":

supermarket.loc[:10, 'Total'].plot(figsize=(14, 6))

This results in an object like this: It turns out that this object does not have the show() method to be able to visualize it. Is there no "direct" way to visualize such an object? I’ve read that can be sent to a Tkinter.Canvas but still not understood how...

1 answer

0


Every object Axes has an object Figure which contains it (starting from the principle that it was initialized correctly). A Figure may contain multiple Axes, in the case of a figure with more than one graph. To display a Figure on screen, use:

figure.show()

In case you only have access to Axes, it is possible to pick up your Figure corresponding with:

ax.get_figure()

It is worth noting that the module pyplot is a shortcut that aims to facilitate this interface of elements: you can take the Figure current (i.e., last modified) with pyplot.gcf(), plot in the Axes current with pyplot.plot() and show the Figure current with pyplot.show(). Accessing the graphical elements Figure and Axes directly is more labor intensive and should only be used if you really need this control.

In your case, call DataFrame.plot() is even higher level than using the pyplot: pandas itself cares to call/initialize the matplotlib elements for you. In general, to avoid the verbosity of something like:

ax = df.plot( ... )
ax.get_figure().show()

Prefer:

df.plot( ... )
plt.show()

Having previously imported the pyplot with the convention import matplotlib.pyplot as plt.

Here is a more detailed description of the elements that make up a Axis.

Here talks a little more about the functioning of the module pyplot.

  • Great. It worked, that’s right. I had read a lot of documents without getting results. You have managed to explain the basics to me and direct me in the right direction to read what is needed. Now the rest is up to me. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.