Matplotlib: implementing Customizing Legends for Multiple Line Plots in Different Colors
I'm having trouble with I'm experimenting with I'm sure I'm missing something obvious here, but I'm stuck on something that should probably be simple. I've been banging my head against this for hours. I'm trying to create a line plot using Matplotlib where I have multiple lines in different colors, but I'm running into issues when customizing the legend. I'm using Matplotlib version 3.6.0 and want to use a custom label for each line while ensuring the legend reflects the respective colors correctly. Here's a snippet of my code: ```python 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) def plot_lines(): plt.plot(x, y1, color='red', label='Sine Wave') plt.plot(x, y2, color='blue', label='Cosine Wave') plt.legend(loc='upper right') plt.title('Sine and Cosine Waves') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.grid(True) plt.show() plot_lines() ``` When I run this code, the plot displays correctly, but when I try to customize the legend further, such as adding a background color or changing the font size, it seems to ignore those options. For example, I tried adding `plt.legend(loc='upper right', fontsize='large', facecolor='lightgrey')`, but it didn’t change the appearance as expected. Additionally, I’m curious if there’s a way to add a border around the legend or to adjust the spacing between entries in the legend. I’ve looked through the documentation but can’t find clear examples for these features. Any help with customizing the legend effectively would be greatly appreciated! For context: I'm using Python on Ubuntu. My development environment is Linux. Has anyone else encountered this? Am I missing something obvious? Any ideas how to fix this? I've been using Python for about a year now. Am I approaching this the right way?