Метод 1: Matplotlib (Python)
Matplotlib — это широко используемая библиотека визуализации данных в Python. Чтобы добавить заголовок легенды, вы можете использовать метод titleобъекта легенды. Вот пример:
import matplotlib.pyplot as plt
# Create a plot
x = [1, 2, 3]
y = [4, 5, 6]
plt.plot(x, y, label='Data')
# Add legend
legend = plt.legend(title='Legend Title')
legend.get_title().set_fontsize('12') # Adjust the font size
# Display the plot
plt.show()
Метод 2: ggplot2 (R)
ggplot2 — это популярный пакет визуализации данных в R. Чтобы добавить заголовок легенды, вы можете использовать функцию labs. Вот пример:
library(ggplot2)
# Create a plot
df <- data.frame(x = c(1, 2, 3), y = c(4, 5, 6))
ggplot(df, aes(x, y, color = "Data")) +
geom_line() +
labs(color = "Legend Title")
# Display the plot
const ctx = document.getElementById('myChart').getContext('2d');
const chart = new Chart(ctx, {
type: 'line',
data: {
labels: ['A', 'B', 'C'],
datasets: [{
label: 'Data',
data: [4, 5, 6],
borderColor: 'red',
backgroundColor: 'transparent',
}]
},
options: {
plugins: {
legend: {
title: {
text: 'Legend Title',
display: true,
font: {
size: 12
}
}
}
}
}
});