import matplotlib.pyplot as plt import numpy as np # Sample Data x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) # Create Plot fig, ax = plt.subplots() ax.plot(x, y1, label='Sine Wave') ax.plot(x, y2, label='Cosine Wave') ax.legend() plt.show()
import matplotlib.pyplot as plt import numpy as np # Sample Data x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) fig, ax = plt.subplots() ax.plot(x, y1, label='Sine Wave') ax.plot(x, y2, label='Cosine Wave') ax.legend(loc='upper right') # Change legend position plt.show()
import matplotlib.pyplot as plt import numpy as np # Sample Data x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) fig, ax = plt.subplots() ax.plot(x, y1, label='Sine Wave') ax.plot(x, y2, label='Cosine Wave') legend = ax.legend(loc='upper right') legend.set_title('Waves') # Set legend title plt.show()
import matplotlib.pyplot as plt import numpy as np # Sample Data x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) fig, ax = plt.subplots() ax.plot(x, y1, label='Sine Wave') ax.plot(x, y2, label='Cosine Wave') ax.legend(loc='upper right', fontsize='large', title='Waves', title_fontsize='medium') plt.show()
import matplotlib.pyplot as plt import numpy as np # Sample Data x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) fig, ax = plt.subplots() line1, = ax.plot(x, y1, label='Sine Wave') line2, = ax.plot(x, y2, label='Cosine Wave') # Custom legend handles and labels ax.legend(handles=[line1], labels=['Only Sine']) plt.show()
import matplotlib.pyplot as plt import numpy as np # Sample Data x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) fig, ax = plt.subplots() ax.plot(x, y1, label='Sine Wave', color='blue') ax.plot(x, y2, label='Cosine Wave', color='green') # Customizing legend colors legend = ax.legend() for text, line in zip(legend.get_texts(), legend.get_lines()): text.set_color(line.get_color()) plt.show()
import matplotlib.pyplot as plt import numpy as np # Sample Data x = np.linspace(0, 10, 100) y1 = np.sin(x) y2 = np.cos(x) fig, ax = plt.subplots() ax.plot(x, y1, label='Sine Wave') ax.plot(x, y2, label='Cosine Wave') # Place legend outside of the plot ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) plt.show()