'Making a line plot with values of a row
I have a dataframe of this type:
| Group_Number | Week_1 | Week_2 | Week_3 |
|---|---|---|---|
| 1 | 51% | 20% | 12% |
| 2 | 21% | 87% | 40% |
| 3 | 3% | 10% | 90% |
I am trying to make 2 line plots with pandas that shows the week values of only a specific group, but when plotting it shows nothing because instead it makes one line per week. Also, since there is only one value per week it shows nothing because it is only a point in a plot.
Do you have any suggestions to crack this one?
I tried this and didn't work:
df[df["Group_Number"] == 1 ].plot(y = ["Week_1", "Week_2", "Week_3"], kind = "line")
plt.show()
Thanks to everyone in advance <3
Solution 1:[1]
One solution is by transposing the DataFrame such that your Group_number become column headers and Week_1,Week_2.. become indices.
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
new_dict = {"Group":[1,2,3],"Week1":[51,20,12],"Week2":[21,87,40],"Week3":[3,10,90]}
my_new_df = pd.DataFrame(new_dict)
my_new_df = my_new_df.set_index("Group")
transposed = my_new_df.T
# Plotting with seaborn
fig,ax = plt.subplots()
sns.lineplot(x=transposed.index,y=transposed[1],marker='*',ax = ax)
plt.show()
fig.savefig("debug_lineplot.png")
Now you can loop over the indices to plot other groups on same/different figures.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

