Pandas in PyCharm: Where does it display the boxplot?

Viewed 1657

I am making a python script using the PyCharm IDE, and the idea is to display descriptive statistics and a box plot for each group in a DataFrame. The statistics displays, but the boxplot is nowhere to be seen...

I have tried Googling an answer, but it does not seem this question have been answered before.

    import pandas as pd
    import matplotlib as plt

    (...)

    for name, group in grouped:
        if len(group) > 3:
            print("\n\nNAME: {}".format(name))
            print("GROUP: {}".format(group))
            print("DESCRIPTIVE STATISTICS 
{}".format(group.distance2.describe()))
            print(group.distance2.plot.box())
            group.distance2.plot.box()

I do not get any error messages, the code runs and completes, but I do not know where the boxplot is supposed to display.

1 Answers

I think the code as it is does not create a matplotlib figure object. Try creating a test data object for group.distance2, then create a matplotlib boxplot object. I am assuming you are using the matplotlib library.

import matplotlib.pyplot as plt

for name, group in grouped:
    if len(group) > 3:
        data = group.distance2
        # create a matplotlib figure object
        fig, axs = plt.subplots(1, 1)
        # basic plot
        axs[0, 0].boxplot(data)
        axs[0, 0].set_title('basic plot of group.distance2')
        plt.show()

It that works, you can try putting several group data into one figure (axes). Here is more information: https://matplotlib.org/3.1.0/gallery/statistics/boxplot_demo.html

Related