PYTHON
matplotlib legend out of plot
ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05),
ncol=3, fancybox=True, shadow=True)
matplotlib legend
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 20, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, "-b", label="sine")
plt.plot(x, y2, "-r", label="cosine")
plt.legend(loc="upper left")
plt.ylim(-1.5, 2.0)
plt.show()
position of legend matplotlib
plt.legend(loc='upper right')
place legend on location matplotlib
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = 6, 3
fig, axes = plt.subplots(ncols=3)
locs = ["upper left", "lower left", "center right"]
for l, ax in zip(locs, axes.flatten()):
ax.set_title(l)
ax.plot([1,2,3],[2,3,1], "b-", label="blue")
ax.plot([1,2,3],[1,2,1], "r-", label="red")
ax.legend(loc=l, bbox_to_anchor=(0.6,0.5)) # This is the key statement.
ax.scatter((0.6),(0.5), s=81, c="limegreen", transform=ax.transAxes)
plt.tight_layout()
plt.show()
matplotlib position legend
plt.legend(loc='best')
plt.legend(loc='upper right')
plt.legend(loc='Center left')
plt.legend(loc='Upper center')
# etc.
legend matplotlib
import matplotlib.pyplot as plt
#define x and ysin
plt.plot(x,ysin,label='sin(x)')
plt.legend()
plt.show()
legend ax matplotlib
ax.plot([1, 2, 3], label='Inline label')
ax.legend()
matplotlib legend location manual
answer=this is how we are defining the location of legend
matplotlib legend out of plot
ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.05),
ncol=3, fancybox=True, shadow=True)
matplotlib legend
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 20, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, "-b", label="sine")
plt.plot(x, y2, "-r", label="cosine")
plt.legend(loc="upper left")
plt.ylim(-1.5, 2.0)
plt.show()
position of legend matplotlib
plt.legend(loc='upper right')
place legend on location matplotlib
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = 6, 3
fig, axes = plt.subplots(ncols=3)
locs = ["upper left", "lower left", "center right"]
for l, ax in zip(locs, axes.flatten()):
ax.set_title(l)
ax.plot([1,2,3],[2,3,1], "b-", label="blue")
ax.plot([1,2,3],[1,2,1], "r-", label="red")
ax.legend(loc=l, bbox_to_anchor=(0.6,0.5)) # This is the key statement.
ax.scatter((0.6),(0.5), s=81, c="limegreen", transform=ax.transAxes)
plt.tight_layout()
plt.show()
matplotlib position legend
plt.legend(loc='best')
plt.legend(loc='upper right')
plt.legend(loc='Center left')
plt.legend(loc='Upper center')
# etc.
legend matplotlib
import matplotlib.pyplot as plt
#define x and ysin
plt.plot(x,ysin,label='sin(x)')
plt.legend()
plt.show()
legend ax matplotlib
ax.plot([1, 2, 3], label='Inline label')
ax.legend()
matplotlib legend location manual
answer=this is how we are defining the location of legend