Reputation: 199
Here is my code.
import numpy as np
from matplotlib import pyplot as plt
fig = plt.figure()
plt.plot(np.arange(0.0,12.0,2.0), 'r-', label='P = increasing')
plt.plot(np.arange(10.0,0.5,-1.8), 'g-', label='P = decreasing')
plt.legend()
plt.show()
The output is shown in the following figure.,
But I want to modify the legend so that it can be shown as,
How to do that?
Upvotes: 2
Views: 1966
Reputation: 493
This obtains the output you show in the image.
fig, ax = plt.subplots()
ax.plot(np.arange(0.0,12.0,2.0), 'r-')
ax.plot(np.arange(10.0,0.5,-1.8), 'g-')
handles, labels = ax.get_legend_handles_labels()
labels = ['P = increasing', ' = decreasing']
ax.legend(handles, labels, loc = 'center right', markerfirst = False)
plt.show()
Upvotes: 1
Reputation: 18218
You can try:
plt.legend(markerfirst = False)
From documentation:
markerfirst : bool
If True, legend marker is placed to the left of the legend label. If False, legend marker is placed to the right of the legend label. Default is True.
Upvotes: 4