Introduction
Labels, titles, and legends help explain what a chart or graph shows. They make data easy to understand.
Labels, titles, and legends help explain what a chart or graph shows. They make data easy to understand.
import matplotlib.pyplot as plt plt.plot(x, y, label='Line 1') plt.xlabel('X-axis label') plt.ylabel('Y-axis label') plt.title('Chart Title') plt.legend() plt.show()
xlabel() and ylabel() to add labels to the x and y axes.title() to add a main title to the chart.legend() to show the labels for different data series.plt.plot([1, 2, 3], [4, 5, 6]) plt.xlabel('Time') plt.ylabel('Value') plt.title('Simple Line Chart') plt.show()
plt.plot([1, 2, 3], [4, 5, 6], label='Data 1') plt.plot([1, 2, 3], [6, 5, 4], label='Data 2') plt.legend() plt.title('Two Lines with Legend') plt.show()
This program plots two lines showing sales for two years. It adds labels for the x and y axes, a title, and a legend to explain the lines.
import matplotlib.pyplot as plt # Data x = [1, 2, 3, 4] y1 = [10, 20, 25, 30] y2 = [15, 18, 22, 28] # Plot first line plt.plot(x, y1, label='Sales 2023') # Plot second line plt.plot(x, y2, label='Sales 2024') # Add labels and title plt.xlabel('Quarter') plt.ylabel('Revenue (in thousands)') plt.title('Quarterly Sales Comparison') # Show legend plt.legend() # Display the plot plt.show()
Labels explain what each axis means.
Titles give the chart a clear heading.
Legends describe different data lines or groups.
title to a chart in data visualization?plt.legend() adds a legend to the plot.import matplotlib.pyplot as plt
plt.plot([1, 2, 3], label='Line 1')
plt.plot([3, 2, 1], label='Line 2')
plt.title('Sample Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.legend()
plt.show()plt.legend() displays the legend with the given labels. plt.show() displays the plot.import matplotlib.pyplot as plt plt.plot([1, 2, 3]) plt.plot([3, 2, 1]) plt.legend() plt.show()
label argument, so legend has no text to display.plt.legend() without labels results in an empty legend. plt.show() is present and plt.title() is not required.import pandas as pd
import matplotlib.pyplot as plt
data = {'Month': ['Jan', 'Feb', 'Mar'], 'ProductA': [100, 120, 130], 'ProductB': [90, 110, 115]}
df = pd.DataFrame(data)
# Which code below is correct?
A: plt.plot(df['Month'], df['ProductA'], label='Product A')
plt.plot(df['Month'], df['ProductB'], label='Product B')
plt.title('Sales Over Months')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.legend()
plt.show()
B: plt.plot(df['ProductA'], df['Month'], label='Product A')
plt.plot(df['ProductB'], df['Month'], label='Product B')
plt.title('Sales Over Months')
plt.xlabel('Sales')
plt.ylabel('Month')
plt.legend()
plt.show()
C: plt.plot(df['Month'], df['ProductA'])
plt.plot(df['Month'], df['ProductB'])
plt.title('Sales Over Months')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()
D: plt.plot(df['Month'], df['ProductA'], label='Product A')
plt.plot(df['Month'], df['ProductB'], label='Product B')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()label in plot calls and calls plt.legend() and plt.title(). Others miss legend or swap axes.Pass the test and this pattern moves unknown