Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse legend order pandas plot

I have following code to show stacked bar

handles = df.toPandas().set_index('x').T.plot(kind='bar', stacked=True, figsize=(11,11))
    plt.legend(loc='best', title="Line", fontsize = 'small', framealpha=0)
    plt.ylabel("'" + lineName + "'")
    plt.show()

I want to reverse the order of legend I used handles=handles[::-1]but I got an error.

like image 570
Yakov Avatar asked Feb 12 '16 23:02

Yakov


People also ask

How to reverse the Order of Legends in Python guides?

Inside guides () function, we take the parameter color, which will call guide_legend () guide function as value. Inside guide_legend () function, we take an argument called reverse, which is a logical parameter. If “reverse = TRUE”, the order of legends is reversed otherwise it will remain as it was. Syntax : guides (…)

How to reverse the Order of labels in a Matplotlib legend?

To reverse the order of the labels, we first grab its handles and set the handles and labels in reversed order: import matplotlib.pyplot as pyplot # Draw a stackplot pyplot.stackplot (x, y_vals, labels = labels) # Reverse the order of labels in legend pyplot.legend (reversed (pyplot.legend ().legendHandles), reversed (labels))

How to reverse the Order of legend in Geom_point?

To Reverse the order of Legend, we have to add guides () and guide_legend () functions to the geom_point () function. Inside guides () function, we take the parameter color, which will call guide_legend () guide function as value. Inside guide_legend () function, we take an argument called reverse, which is a logical parameter.

How to keep the index order in a reversed Dataframe in Python?

Example 2 demonstrates how to keep the index order in a reversed DataFrame using the reset_index function in Python. Have a look at the Python code below: The values in our reversed DataFrame are the same as in Example 1. However, this time the indices in the reversed data table are still ranging from 0 to the number of rows.


1 Answers

DataFrame.plot takes a legend argument, which can be True/False/'reverse'. You want legend='reverse'

like image 123
TomAugspurger Avatar answered Oct 24 '22 23:10

TomAugspurger