Home > Net >  matplotlib - pandas - How to create multiple-labels in one plot?
matplotlib - pandas - How to create multiple-labels in one plot?

Time:05-10

This is my code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import randn

df = pd.read_csv(r"XXXXXXX.txt")

df.plot(x='qPkw', y='vPkw', kind='scatter', figsize=(12, 12), use_index=True,
        title="q-v-Diagram (Pkw)", xticks=[0, 25, 50, 75, 100, 125], yticks=[0, 100, 200, 300, 400, 500],
        xlabel="v-Pkw", ylabel="q-Pkw", fontsize=15, color="black", label="Pkw")

df.plot(x='qLkw', y='vLkw', kind = 'scatter', figsize=(12, 12), use_index=True,
        title="q-v-Diagram (Lkw)", xticks=[0, 5, 10, 15, 20, 25, 30], yticks=[0, 100, 200, 300, 400],
        xlabel="v-Lkw", ylabel="q-Lkw", fontsize= 15, color="blue", label="Lkw")

plt.show()

Instead of one, I get two plots.

I just want to have these two in one plot. Also I want to label these two.

Someone sees the mistake?

CodePudding user response:

Capture the first axis, and use the ax keyword in the second plot with the first axis.

ax = df.plot(x='qPkw', y='vPkw', kind='scatter', figsize=(12, 12), use_index=True,
        title="q-v-Diagram (Pkw)", xticks=[0, 25, 50, 75, 100, 125], yticks=[0, 100, 200, 300, 400, 500],
        xlabel="v-Pkw", ylabel="q-Pkw", fontsize=15, color="black", label="Pkw")

df.plot(x='qLkw', y='vLkw', kind = 'scatter', figsize=(12, 12), use_index=True,
        title="q-v-Diagram (Lkw)", xticks=[0, 5, 10, 15, 20, 25, 30], yticks=[0, 100, 200, 300, 400],
        xlabel="v-Lkw", ylabel="q-Lkw", fontsize= 15, color="blue", label="Lkw", 
        ax=ax)

Be aware that the first xticks and yticks settings will be ignored. The same for the xlabel and ylabel. That has to be, since it is in one figure.

  • Related