0

Following up from a previous question, I am confused by how matplotlib.pyplot works. I have a DataFrameGroupBy object with multiple groups, and I want to make a single figure with multiple functions (one per group). My research indicates that the following code should essentially yield one figure with multiple lines:

import pandas as pd
import matplotlib.pyplot as plt

with open("data.csv", encoding='utf-8') as datei:
    df = pd.read_csv(datei, sep=';')
    groups = df.groupby(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                         'L', 'M', 'N', 'O', 'P', 'Q'])  # Columns from A-Q are distinctive as a unit

    for element, group in groups:  # Looping over the GroupBy objects
        group.plot(x='R', y='X')  # Columns R and X contain the x- and y-axis values respectively

    # According to SO answers, calling .plot() should not create a graph until the graph itself is called
    plt.show()

Instead, I am getting multiple graphs with one line each. Following some instructions, I tried shoving plt.ion(), plt.ioff(), and plt.pause(0.5) in all possible places that I could think of, but I don't quite understand how they work, so I might have not done it correctly.

  • Are you running the code in Jupyter Notebook? I found a that matplotlib.pyplot does not wait for `plt.show()` to show graphs in notebook versus other IDEs. – Jonas Palačionis Oct 14 '20 at 11:21
  • No, I access Spyder via the Anaconda Prompt. I was told at university that this was the best option out there, and I sadly do not know much about how Python code runs where to do otherwise. – ubhalasairde Oct 14 '20 at 11:26

2 Answers2

0

You are using pandas' plot function. You can instruct this function which axes to use by passing a reference to the Axes using ax=.

import pandas as pd
import matplotlib.pyplot as plt

with open("data.csv", encoding='utf-8') as datei:
    df = pd.read_csv(datei, sep=';')
groups = df.groupby(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                     'L', 'M', 'N', 'O', 'P', 'Q'])  # Columns from A-Q are distinctive as a unit

fig, ax = plt.subplots()  # creates one figure with one axes
for element, group in groups:  # Looping over the GroupBy objects
    group.plot(x='R', y='X', ax=ax)  # Columns R and X contain the x- and y-axis values respectively

# According to SO answers, calling .plot() should not create a graph until the graph itself is called
plt.show()
Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75
0

I was able to plot multiple data using the following:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = sns.load_dataset('iris')
groups = df.groupby('species')

fig = plt.figure() # creating figure object
ax = fig.add_axes([0,0,1,1]) # adding axes to it, sort of a blank canvas

for _,group in groups: # iterating over different groups and painting selected metrics
    ax.plot(group['sepal_length'], group['sepal_width'], label=group['species'].iloc[0])
    plt.legend()

enter image description here

Jonas Palačionis
  • 4,591
  • 4
  • 22
  • 55