Skip to content Skip to sidebar Skip to footer

Row Based Chart Plot (Seaborn Or Matplotlib)

Given that my data is a pandas dataframe and looks like this: Ref +1 +2 +3 +4 +5 +6 +7 2013-05-28 1 -0.44 0.03 0.06 -0.31 0.13 0.56 0.81

Solution 1:

Plotting a dataframe in pandas is generally all about reshaping the table so that the individual lines you want are in separate columns, and the x-values are in the index. Some of these reshape operations are a bit ugly, but you can do:

df = pd.read_clipboard()
plot_table = pd.melt(df.reset_index(), id_vars=['index', 'Ref'])
plot_table = plot_table.pivot(index='variable', columns='Ref', values='value')
# Add extra row to have all lines start from 0:
plot_table.loc['+0', :] = 0
plot_table = plot_table.sort_index()
plot_table
Ref          1     2     3     4     5
variable                              
+0        0.00  0.00  0.00  0.00  0.00
+1       -0.44  0.84  0.09  0.35  0.09
+2        0.03  1.03  0.25  1.16 -0.10
+3        0.06  0.96  0.06  1.91 -0.38
+4       -0.31  0.90  0.09  3.44 -0.69
+5        0.13  1.09 -0.09  2.75 -0.25
+6        0.56  0.59 -0.16  1.97 -0.85
+7        0.81  1.15  0.56  2.16 -0.47

Now that you have a table with the right shape, plotting is pretty automatic:

plot_table.plot()

Post a Comment for "Row Based Chart Plot (Seaborn Or Matplotlib)"