简体   繁体   中英

Why did matplotlib.figure.Figure object store the data from a following pandas.DataFrame.plot command?

Newbie here.Out of all the Python modules that I have used and learnt so far, I am finding matplotlib the most confusing one. I believe this is because of the two interfaces that exist: state-based based on MATLAB Vs object-oriented. At a class I took we used state-based, whereas I find the object-oriented approach in general a lot more logical (and recommended by the matplotlib documentation).

I am using the code I posted below on a jupyter notebok (code was extracted from here ).

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_excel("https://github.com/chris1610/pbpython/blob/master/data/sample-salesv3.xlsx?raw=true")

top_10 = (df.groupby('name')['ext price', 'quantity'].agg({'ext price': 'sum', 'quantity': 'count'})
          .sort_values(by='ext price', ascending=False))[:10].reset_index()
top_10.rename(columns={'name': 'Name', 'ext price': 'Sales', 'quantity': 'Purchases'}, inplace=True)


fig, ax = plt.subplots()
ax.set_xlim([-10000, 140000])
ax.set_xlabel('Total Revenue')
ax.set_ylabel('Customer')
top_10.plot(kind='barh', y="Sales", x="Name", ax=ax)

My question is the following: if after the previous code I run a new block with just:

fig

it reprints the whole plot, including the data from the top_10 DataFrame. How did the data from the DataFrame become part of my fig object?

It is because, pandas plot() function inherently uses matplotlib's plot() function. And a new plot is initialized with fig, ax = plt.subplots() in your code. So, if you want a new plot you can use fig, ax = plt.subplots() after top_10.plot(kind='barh', y="Sales", x="Name", ax=ax)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM