0

This is what my dataframe looks like: click on the line to download dataframe

enter link description here for dataframe

enter image description here

I have tried the following code :

plt.plot(LessDF['DeptAvg'][LessDF['classes'] == 'COA111'], LessDF['week1'])
plt.plot(LessDF['DeptAvg'][LessDF['classes'] == 'COA111'], LessDF['week2'])
plt.plot(LessDF['DeptAvg'][LessDF['classes'] == 'COA111'], LessDF['week3'])

I got the output below, which shows only one line, with my code. enter image description here

I want output with separate lines, like this: enter image description here

How can I get this output with matplotlib or seaborn??

6
  • Does this answer your question? How to plot multiple dataframes in subplots Commented Jan 20, 2023 at 10:44
  • No this can't helpful to me @T C Molenaar your suggested solution is for subplot on same cell. I want multiple plot on one subplot. Commented Jan 20, 2023 at 10:48
  • 1
    You should provide an example of your data to make your issue reproducible. It is not clear what LessDF looks like. It might be that your lines are just overlapping. stackoverflow.com/help/minimal-reproducible-example Commented Jan 20, 2023 at 10:57
  • 1
    I have updated the question & now can view the dataframe also. Commented Jan 20, 2023 at 11:00
  • images aren't ideal for sharing data but thanks Commented Jan 20, 2023 at 11:15

4 Answers 4

1

With Seaborn, with its object interface available from v0.12, you might do like this:

import pandas as pd
import seaborn as sns
import seaborn.objects as so

sns.set_theme()

First, convert the data frame into a long-form for easier processing in the second figure.

df = pd.read_csv("LessDF.csv", index_col=0)

df_long = (
    # Convert to a long-form
    pd.melt(df,
        id_vars=["Id", "classes", "LessAvg", "DeptAvg"],
        var_name=["week"],
        value_name="point"
    )

    # Make `week1` to `1`
    .assign(week=lambda df_: df_.week.str.replace("week", ""))
)

Then

(
    so.Plot(
        # We don't have to drop rows but since `DeptAvg` doesn't change
        # over `classes` and `week`, we can de-duplicate them
        df_long.drop_duplicates(["classes", "week"]),
        x="week", y="DeptAvg", color="classes"
    )
    .add(so.Line())
    .limit(y=(0, 100))
)

enter image description here

If you'd like to also render the individual Id's point of each week, you might do something like this:

(
    so.Plot(data=df_long, x="week", y="point", color="classes")
    .add(so.Dots(), so.Dodge(), so.Jitter(.3))
    .add(so.Line(linewidth=3, alpha=.8), y="DeptAvg")
    .limit(y=(0, 100))
)

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

This is the exact solution i wated @ryu 1 kn Thanks. This will helps me to solve my many of doubts.
1

All your values in the DeptAvg column are 67 for the filter you applied.

Also, you are providing a boolean as your x: LessDF['DeptAvg'] == 'COA111'.

Also, you are applying the condition on the wrong column DeptAvg instead of classes

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_csv('../../../Desktop/LessDF.csv')
df_filtered = df[df['classes'] == 'COA111' ]

plt.plot(df_filtered['week1'],df_filtered['DeptAvg'],alpha=.5,)
plt.plot(df_filtered['week2'],df_filtered['DeptAvg'],alpha=.5)
plt.plot(df_filtered['week3'],df_filtered['DeptAvg'],alpha=.5)

plt.legend(['week1','week2','week3'])

plt.show()

enter image description here

more info here

3 Comments

what are you talking about? the answer is given..
My intention was not to you @Claudio Paladini
This solution is helpful to me @Claudio Paladini. Thanks
0
# I done this using seaborn you can use matplotlib in between to code
plt.figure(figsize=(16, 16)) 
plt.subplot(no_of_rows, no_of_columns, plot_num)
plt.title('Any title 1')
sns.boxplot(df['column_name'])

Example :- we want 2 rows with columns of plots then we use
plt.subplot(2, 2, 1)
plt.title('Any title 1')
sns.distplot(df['column_name'], bins=20)

plt.subplot(2, 2, 2)
plt.title('Any title 2')
sns.distplot(df['column_name'], bins=20)

plt.subplot(2, 2, 3)
plt.title('Any title 3')
sns.distplot(df['column_name'], bins=20)

plt.subplot(2, 2, 4)
plt.title('Any title 4')
sns.distplot(df['column_name'], bins=20)

plt.show()

1 Comment

as per the comments to the questions, he is not looking for subplots. Instead, he is looking for multiple lines in one plot.
0
fig, ax = plt.subplots(2,3, figsize=(16,10))

sns.boxplot(df,y="arpu_6",ax=ax[0,0])
ax[0,0].set_title("June ARPU")
ax[0,0].set_ylim([0, 5000])

sns.boxplot(df,y="arpu_7",ax=ax[0,1])
ax[0,1].set_title("July ARPU")
ax[0,1].set_ylim([0, 5000])

sns.boxplot(df,y="arpu_8",ax=ax[0,2])
ax[0,2].set_title("Aug ARPU")
ax[0,2].set_ylim([0, 5000])

I picked off the code from my own notebook, so doesn't match your dataframe, so just modify as per your needs.

Comments

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.