设为首页 加入收藏

TOP

【matplotlib基础】--图例(一)
2023-09-09 10:25:28 】 浏览:122
Tags:matplotlib 基础 图例

Matplotlib 中的图例是帮助观察者理解图像数据的重要工具。
图例通常包含在图像中,用于解释不同的颜色、形状、标签和其他元素。

1. 主要参数

当不设置图例的参数时,默认的图例是这样的。

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.plot(x, y1, label="sin")
ax.plot(x, y2, label="cos")
ax.legend()

image.png

图例就是右上角的那个部分。
图例的主要参数,其实也就是上例 ax.lengend() 函数的主要参数:

  1. 图例位置相关:loc (位置字符串)
  2. 边框相关:facecolor(背景色),edgecolor(边框颜色),shadow(是否设置阴影)framemon(是否有边框和背景)
  3. 图例的列数:默认是1列多行的格式,ncol(列的个数)

2. 配置示例

通过示例来演示常用的设置。

2.1. 图例位置

fig, ax = plt.subplots(3, 3)
fig.set_size_inches(10, 10)

locations = [
    ["lower left", "lower center", "lower right"],
    ["center left", "center", "center right"],
    ["upper left", "upper center", "upper right"],
]
for i in range(3):
    for j in range(3):
        ax[i, j].plot(x, y1, label="sin")
        ax[i, j].plot(x, y2, label="cos")
        ax[i, j].legend(loc=locations[i][j])

image.png

上面的示例显示了不同位置的图例。

2.2. 图例边框

边框可以设置边框的背景色,边框颜色和是否有阴影。

fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.plot(x, y1, label="sin")
ax.plot(x, y2, label="cos")
ax.legend(facecolor="lightblue", edgecolor="red", shadow=True)

image.png

上例中,背景色 lightblue,边框 red,阴影设置为 True

设置无边框比较简单,frameon=False 即可。

fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.plot(x, y1, label="sin")
ax.plot(x, y2, label="cos")
ax.legend(frameon=False)

image.png

2.3. 图例分列

图例默认都是一列多行的格式,比如上面的的各个示例,图例都是依次竖着排列下来的。
可以通过 ncol 属性,让图例横着排列。

fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.plot(x, y1, label="sin")
ax.plot(x, y2, label="cos")
ax.legend(frameon=False, loc="upper center", ncol=2)

image.png

上面的示例,图例(legend)设置为两列,位于上方中间位置。

2.4. 多个图例

一般的图形都只有一个图例,比如上面的都是这样的,sincos都在一个图例中。
如果图例太多,或者多个图例之间关系不大,也可以创建多个图例。

from matplotlib.legend import Legend

x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.sin(x + 1)
y4 = np.cos(x + 1)

fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
legends = []
legends += ax.plot(x, y1, label="sin1")
legends += ax.plot(x, y2, label="cos1")
legends += ax.plot(x, y3, label="sin2")
legends += ax.plot(x, y4, label="cos2")
ax.legend(legends[:2], ["sin1", "cos1"], loc="upper right")

leg = Legend(ax, legends[2:], ["sin2", "cos2"], loc="lower left")
ax.add_artist(leg)

image.png

上面的示例中的4个曲线,分成了2个图例来说明。
一个图例在右上角,一个图例在左下角。

2.5. 图例中不同大小的点

最后,介绍一种更复杂的图例显示方式。

首先生成主要几个省市的人口散点图(数据是网络上搜索的),
生成图例的时候,给3个主要的节点500万人,5000万人,1亿人设置的点的大小比例与图中的各个散点数据保持一致。

x = ["广东", "山东", "江苏", 
     "湖北", "浙江", "吉林", 
     "甘肃", "宁夏", "青海", "西藏"]
y = np.array([10432, 9578, 7866, 
              5723, 5442, 2745,
              2557, 630, 562, 300])

fig = plt.figure(figsize=[10, 8])
plt.scatter(x, y, c=np.log10(y), s=y/16)

#创建图例
for population in [500, 5000, 10000]:
    plt.scatter([],[], c='b', 
                s=population/16, 
                alpha=0.3, 
                label=str(population)+" (万人)")

plt.legend(scatterpoints=1, 
           labelspacing=1.5, 
           title="人口图例&
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Pycharm的安装和使用 下一篇Python实现京东茅台抢购脚本, 原..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目