📜  如何更改 Matplotlib 图例中的项目顺序?

📅  最后修改于: 2022-05-13 01:54:44.905000             🧑  作者: Mango

如何更改 Matplotlib 图例中的项目顺序?

Legend 是 Matplotlib 包中的一种方法,它表示描述绘图/图形中元素的区域。

图例区域中标签的顺序基于我们将线(数据)添加到图中的顺序。

例子:

让我们考虑一个在每周测试中存储学生分数的 DataFrame。

 

Raju

Hari

Bablu

Dora

Test-1

8

6

9

10

Test-2

10

4

9

9

Test-3

7

6

9

10

Test-4

7

7

9

9

Test-5

10

6

9

10

上表存储了 5 个测试中的 4 个学生分数。现在使用上述数据绘制带有图例的图形。

Python3
# import necessary packages
import pandas as pd
import matplotlib.pyplot as plt
  
# create a dataframe
Marks = pd.DataFrame({'Raju': [8, 10, 7, 7, 10],
                      'Hari': [6, 4, 6, 7, 6],
                      'Bablu': [9, 9, 9, 9, 9],
                      'Dora': [10, 9, 10, 9, 10]})
  
# plot marks of each student
plt.plot(Marks['Raju'], label="Raju Marks", color="Red")
plt.plot(Marks['Hari'], label="Hari Marks", color="Blue")
plt.plot(Marks['Bablu'], label="Bablu Marks", color="Yellow")
plt.plot(Marks['Dora'], label="Dora Marks", color="Black")
  
# labelling the axes
plt.xlabel("Tests")
plt.ylabel("Marks")
  
# add legend to plot
plt.legend()
plt.show()


Python3
# import necessary packages
import pandas as pd
import matplotlib.pyplot as plt
  
# create a dataframe
Marks = pd.DataFrame({'Raju': [8, 10, 7, 7, 10],
                      'Hari': [6, 4, 6, 7, 6],
                      'Bablu': [9, 9, 9, 9, 9],
                      'Dora': [10, 9, 10, 9, 10]})
  
# plot marks of each student
plt.plot(Marks['Raju'], label="Raju Marks", color="Red")
plt.plot(Marks['Hari'], label="Hari Marks", color="Blue")
plt.plot(Marks['Bablu'], label="Bablu Marks", color="Yellow")
plt.plot(Marks['Dora'], label="Dora Marks", color="Black")
  
# labelling the axes
plt.xlabel("Tests")
plt.ylabel("Marks")
  
# reordering the labels
handles, labels = plt.gca().get_legend_handles_labels()
  
# specify order
order = [2, 3, 1, 0]
  
# pass handle & labels lists along with order as below
plt.legend([handles[i] for i in order], [labels[i] for i in order])
plt.show()


Python3
# import necessary packages
import pandas as pd
import matplotlib.pyplot as plt
  
# create a dataframe
Marks = pd.DataFrame({'Raju': [8, 10, 7, 7, 10],
                      'Hari': [6, 4, 6, 7, 6],
                      'Bablu': [9, 9, 9, 9, 9],
                      'Dora': [10, 9, 10, 9, 10]})
  
# plot marks of each student
plt.plot(Marks['Raju'], label="Raju Marks", color="Red")
plt.plot(Marks['Hari'], label="Hari Marks", color="Blue")
plt.plot(Marks['Bablu'], label="Bablu Marks", color="Yellow")
plt.plot(Marks['Dora'], label="Dora Marks", color="Black")
  
# labelling the axes
plt.xlabel("Tests")
plt.ylabel("Marks")
  
# reordering the labels
handles, labels = plt.gca().get_legend_handles_labels()
  
# specify order
order = [3, 2, 1, 0]
  
# pass handle & labels lists along with order as below
plt.legend([handles[i] for i in order], [labels[i] for i in order])
plt.show()


输出

更改图例中项目的顺序

图例区域中元素的上述顺序可以通过gca方法更改,该方法使用另一个称为get_legend_handles_labels方法的子方法。

句法

这些句柄和标签列表作为参数传递给带有索引顺序的图例方法。

请考虑以下示例代码以进行详细了解。

示例 1:

在这段代码中,我们使用了与上述代码相同的 DataFrame 。但是使用上面指定的方法来改变图例区域中元素的顺序。

Python3

# import necessary packages
import pandas as pd
import matplotlib.pyplot as plt
  
# create a dataframe
Marks = pd.DataFrame({'Raju': [8, 10, 7, 7, 10],
                      'Hari': [6, 4, 6, 7, 6],
                      'Bablu': [9, 9, 9, 9, 9],
                      'Dora': [10, 9, 10, 9, 10]})
  
# plot marks of each student
plt.plot(Marks['Raju'], label="Raju Marks", color="Red")
plt.plot(Marks['Hari'], label="Hari Marks", color="Blue")
plt.plot(Marks['Bablu'], label="Bablu Marks", color="Yellow")
plt.plot(Marks['Dora'], label="Dora Marks", color="Black")
  
# labelling the axes
plt.xlabel("Tests")
plt.ylabel("Marks")
  
# reordering the labels
handles, labels = plt.gca().get_legend_handles_labels()
  
# specify order
order = [2, 3, 1, 0]
  
# pass handle & labels lists along with order as below
plt.legend([handles[i] for i in order], [labels[i] for i in order])
plt.show()

输出

示例 2:

下面是在图例区域中以指定顺序绘制表示学生分数的图以及图例的实现。

Python3

# import necessary packages
import pandas as pd
import matplotlib.pyplot as plt
  
# create a dataframe
Marks = pd.DataFrame({'Raju': [8, 10, 7, 7, 10],
                      'Hari': [6, 4, 6, 7, 6],
                      'Bablu': [9, 9, 9, 9, 9],
                      'Dora': [10, 9, 10, 9, 10]})
  
# plot marks of each student
plt.plot(Marks['Raju'], label="Raju Marks", color="Red")
plt.plot(Marks['Hari'], label="Hari Marks", color="Blue")
plt.plot(Marks['Bablu'], label="Bablu Marks", color="Yellow")
plt.plot(Marks['Dora'], label="Dora Marks", color="Black")
  
# labelling the axes
plt.xlabel("Tests")
plt.ylabel("Marks")
  
# reordering the labels
handles, labels = plt.gca().get_legend_handles_labels()
  
# specify order
order = [3, 2, 1, 0]
  
# pass handle & labels lists along with order as below
plt.legend([handles[i] for i in order], [labels[i] for i in order])
plt.show()

输出