Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

matplotlib – pandas – How to create multiple-labels in one plot?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Someone sees the mistake?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading