0

I have a dataframe that looks like this:

import pandas as pd
data = {'Floor': [1, 2, 3, 4], 'IDR 1': [15, 6, 7, 9], 'IDR 2': [36, 12, 78, 23]}
df = pd.DataFrame(data)

df:

   Floor  IDR 1  IDR 2
0      1     15     36
1      2      6     12
2      3      7     78
3      4      9     23

And I want to plot this dataframe as a line chart where y-axis is the floor. I have read the documentation on df.plot() method and it seem you can input more than one columns of data into y kwarg but not to the x.

So I have been reseaching to find a solution and I came across this question that resemles the most to my problem: Plotting multiple columns in a pandas line graph

I have this code: df.plot(kind="line", y=["IDR 1", "IDR 2"], x="Floor", grid=True, xlabel="Floor", ylabel=data)

Basicly I just want to swap the axises.

2 Answers 2

0

Code

use loop

import pandas as pd
import matplotlib.pyplot as plt

cols = ['IDR 1', 'IDR 2']

fig, ax = plt.subplots()

for col in cols:
    df.plot(x=col, y='Floor', ax=ax)
    
plt.xlabel('IDR value')
plt.ylabel('Floor')
plt.legend(labels=cols)
plt.show()

enter image description here

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

Comments

0
import pandas as pd
import matplotlib.pyplot as plt

# Sample data
data = {'Floor': [1, 2, 3, 4], 'IDR 1': [15, 6, 7, 9], 'IDR 2': [36, 12, 78, 23]}
df = pd.DataFrame(data)

# Columns to plot
cols = ['IDR 1', 'IDR 2']

# Create a figure and axis
fig, ax = plt.subplots()

# Plot each column
for col in cols:
    ax.plot(df[col], df['Floor'], marker='o', label=col)

# Set labels and legend
ax.set_xlabel('IDR value')
ax.set_ylabel('Floor')
ax.legend()

# Show the plot
plt.show()

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.