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.
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 (…)
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))
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.
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.
DataFrame.plot
takes a legend
argument, which can be True/False/'reverse'. You want legend='reverse'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With