Python Forum
How does pyplot know what was plotted by the output of pandas.DataFrame(...).cumprod(
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How does pyplot know what was plotted by the output of pandas.DataFrame(...).cumprod(
#1
Hi there,

I am coming from a decade of C# programming to learn python, and trying to wrap my head around how things work.
There seems to be a lot of "magic" in getting the result you want in python.

For example, take the following code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

returns = pd.DataFrame(np.random.normal(1.0, 0.03, (100, 10)))
prices = returns.cumprod()
prices.plot()
plt.title('Randomly-generated Prices')
plt.xlabel('Time')
plt.ylabel('Price')
plt.legend(loc=0);
This produces a nifty line plot, very nice.
So we pass the output of numpy.random.normal(...) into pandas.DataFrame(...) to get, according to the docostring, the following assigned to the variable returns:
Two-dimensional size-mutable, potentially heterogeneous tabular data
structure with labeled axes (rows and columns)


Then somehow that data structure contains a method returns.cumprod() which is called to get, according to the docstring, the following assigned to the variable prices:
Return cumulative cumprod over requested axis.

[Insert googling around and experimenting to work out that this means multiplying the first number in the series by the second number, and then the next one by the output of that, and so on.]

The next four lines of code, (how good would it be if notebook had line numbers), are calling methods of the matplotlib.pyplot lib.

So my question is:
As these are completely separate objects in separate namespaces, how does the matplotlib.pyplot know anything about what pandas.DataFrame(...).comprod(...).plot() just did.

Like in C# one object in one namespace knows nothing about another object in a separate namespace that it hasn't been explicitly told, via input methods.

So just trying to get my head around how this all works, so I can build things with a good design, instead of just copying and pasting stuff I google to find that look like they get the output I want.
Like how does scope work with the above code. Lets say I do two plots, how does it know which one to assign the title to?

Thanks for your time/patience,

Kind Regards.
Reply
#2
For anyone wanting the answer to this question, it was answered in under 10 mins on StackOverflow:
https://stackoverflow.com/questions/6088...m#60885236

Hopefully this helps someone ;-)

Kind Regards.
Reply
#3
Welcome to the forums codeowl, and thank you for sharing the answer!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Add NER output to pandas dataframe dg3000 0 113 Apr-22-2024, 08:14 PM
Last Post: dg3000
  HTML Decoder pandas dataframe column mbrown009 3 1,060 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,130 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,449 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,329 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,273 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,810 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,559 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,321 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 2,459 Jun-22-2021, 07:57 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020