0

i would like to plot multiple density functions in one matplotlib plot. I am currently using:

    def get_probability(self, df):
        df.plot.density() 

Now i got multiple plots in seperated windows. How i am able to get multiple density functions in one plot ?

I would be grateful for any help !

2
  • 2
    Please provide a more detailed question, and examples of what you tried. What is df ? what is plot ? Typically matplotlib can plot multiple function by just performing consecutive plt.plot() calls. Why does this not solve your problem ? Commented Jan 20, 2023 at 10:27
  • 1
    pandas plot functions are a convenience wrapper for matplotlib functions. You should familiarize yourself with the differences between figure and axis objects in matplotlib, predefined an axis object, then plot all density functions into the same axis object. Commented Jan 20, 2023 at 10:33

2 Answers 2

1

If you create, or get the current Axes object, you can pass that to plot.density to plot on the same figure, e.g.,

def get_probability(self, df):
    from matplotlib import pyplot as plt  # you could add this at the top of your script
    ax = plt.gca()  # get current Axes
    df.plot.density(ax=ax)  # pass it to density
Sign up to request clarification or add additional context in comments.

Comments

0

Thank you. I solved the problem by using ax objects.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.