1.多排显示

x=np.linspace(start=-np.pi,stop=np.pi,num=300)

plt.style.use('classic')
Fig,Axes=plt.subplots(1)
Axes.plot(x,np.sin(x),'-b',label='Sine')
Axes.plot(x,np.cos(x),'--r',label='Cosine') Axes.axis('equal')
Axes.legend(loc='lower center',frameon=False,ncol=2) plt.show()

2.指定frameon参数来设定边框

默认情况下图例的边框是开启的,我们可以指定frameon参数来取消边框

x=np.linspace(start=-np.pi,stop=np.pi,num=300)

plt.style.use('classic')
Fig,Axes=plt.subplots(1)
Axes.plot(x,np.sin(x),'-b',label='Sine')
Axes.plot(x,np.cos(x),'--r',label='Cosine') Axes.axis('equal')
Axes.legend(loc='lower center',frameon=False) plt.show()

3.在图例中显示不同尺寸的点

下面我们将以加利福尼亚州所有城市的数据(提取码666)为例来绘图,最终效果是将绘制出各个城市的位置,同时以城市面积大小来使用不同大小的圆表示

cities=pd.read_csv('california_cities.csv')

latitude,longitude=cities['latd'],cities['longd']
population,area=cities['population_total'],cities['area_total_km2'] plt.scatter(latitude,longitude,label=None,c=np.log10(population),cmap='viridis',s=area,linewidths=0,alpha=0.5)
plt.axis(aspect='euqal')a
plt.xlabel('Logitude')
plt.ylabel('Latitude')
plt.colorbar(label='log_{10}$(population)')
plt.clim(3,7) for area in [100,300,500]:
plt.scatter([],[],c='k',alpha=0.3,s=area,label=str(area)+' km$^2$') plt.legend(scatterpoints=1,frameon=False,labelspacing=1,title='City Area')
plt.title('California Cities : Area and Population')
plt.show()

La=1
for color in list('cmyk'):
plt.scatter([],[],c=color,s=100,label=La)
La+=1
plt.legend(frameon=False)
plt.show()

同时显示多个图例

有的时候,由于排版问题,我们可能需要在同一张图像上显示多个图例.但是用Matplotlib来解决这个问题其实并不容易,因为标准的legend接口只支持为一张图像创建一个图例.如果我们使用legend接口再创建第二个,那么第一个图例就会被覆盖

Matplotlib中我们解决这个问题就是创建一个图例艺术家对象,然后调用底层的ax.add_artist()方法来为图片添加第二个图例

Fig,Axes=plt.subplots(1)

lines=[]
style=['-','--','-.',':']
x=np.linspace(start=-np.pi,stop=np.pi,num=500) for i in range(4):
lines+= Axes.plot(x,np.sin(x-i*np.pi/2),style[i],color='black') Axes.axis('equal') Axes.legend(lines[:2],['Line A','Line B'],loc='uppper right',frameon=False)
from matplotlib.legend import Legend
Leg=Legend(Axes,lines[2:],['Line C','Line D'],loc='lower right',frameon=False)
Axes.add_artist(Leg) plt.show()

参考链接:

3.Matplotlib配置图例与颜色条_鸿神的博客-CSDN博客_matplotlib添加颜色条

Matplotlib配置图例legend()设置透明和并排显示的更多相关文章

  1. 【转】matplotlib制图——图例legend

    转自:https://www.cnblogs.com/alimin1987/p/8047833.html import matplotlib.pyplot as pltimport numpy as ...

  2. 【Python学习笔记】调整matplotlib的图例legend的位置

    有时默认的图例位置不符合我们的需要,那么我们可以使用下面的代码对legend位置进行调整. plt.legend(loc='String or Number', bbox_to_anchor=(num ...

  3. gnuplot图例legend设置

    //将图例放在右下角 set key bottom //将图例放在中间 set key center //将图例放在左边 set key left //将图例放在指定位置右下角的坐标为(10,0.7) ...

  4. matplotlib中的legend()—显示图例

    源自  matplotlib中的legend()——用于显示图例 -- 博客园 http://www.cnblogs.com/yinheyi/p/6792120.html legend()的一个用法: ...

  5. matplotlib 进阶之Legend guide

    目录 matplotlib.pyplot.legend 方法1自动检测 方法2为现有的Artist添加 方3显示添加图例 控制图例的输入 为一类Artist设置图例 Legend 的位置 loc, b ...

  6. 妙方之解决matplotlib的图例里的中文呈现小方形

    妙方之解决matplotlib的图例里的中文呈现小方形 分析思路: 每个中文都对应地呈现一个小方形, 不多也不少. 不能说是乱码. 应该是matplotlib的默认字库不支持中文造成的. 应对办法: ...

  7. Android设置透明、半透明等效果

    设置透明效果 大概有三种 1.用android系统的透明效果Java代码 android:background="@android:color/transparent"  例如 设 ...

  8. highcharts图表的图例legend怎么改变显示位置

    一.将图例Legend放于图表右侧1.设置chart的marginRight属性值:chart: { marginRight: 120}2.设置legend图例属性值如下 legend: { alig ...

  9. Matplotlib绘图双纵坐标轴设置及控制设置时间格式

    双y轴坐标轴图 今天利用matplotlib绘图,想要完成一个双坐标格式的图. fig=plt.figure(figsize=(20,15)) ax1=fig.add_subplot(111) ax1 ...

  10. highcharts图表的图例legend

    一.将图例Legend放于图表右侧1.设置chart的marginRight属性值:chart: { marginRight: 120}2.设置legend图例属性值如下 legend: { alig ...

随机推荐

  1. C++20 | std::span 陣列、容器的代理人

    在 C++ 裡頭有相當多「容器」.從原生的陣列,到標準庫 STL 的 vector, array, list, queue, map, set, -.有時候我們只是想以檢視的角度去看一個容器,或是其中 ...

  2. Qt 如何配置CLion标准控制台输出?

    CMake 相关问题: 即CMakeLists.txt文件中,在add_executable添加了WIN32.即当使用了WIN32标识后,就去掉了控制台,那么自然就没有信息打印出来了. # for e ...

  3. Codeforces Round #656 (Div. 3)部分题解

    Codeforces Round #656 (Div. 3)题解 A.Three Pairwise Maximums 解题思路: 依照题意和样例,三个整数x,y,z必须有两个相同且都比第三个数大. 如 ...

  4. 题解 CF1550C. Manhattan Subarrays (思维)

    来源:Educational Codeforces Round 111 (Rated for Div. 2) 不难但很好的思维题 设 \(d(p,q)\) 为 \(p,q\) 两点之间的曼哈顿距离 给 ...

  5. centos7.9 安装oracle11g

    安装环境: 操作系统:CentOS Linux release 7.9.2009 (Core)orcle安装包:linux.x64_11gR2_database_1of2.zip. linux.x64 ...

  6. 2 Englishi 词根

    11 mis 错 misuse misfortune misunderstand 12 out 超过: 外 outnumber outspend outdoor 13 over 过度 overprai ...

  7. BTC-实现

    BTC-实现 Transaction-based ledger(比特币是基于交易的账本模式) Account-based ledger(以太坊是基于账户的账本模式) UTXO Unspent Tran ...

  8. vue3 路由页面返回时,恢复滚动条位置

    首先,路由必须是KeepAlive模式 <script setup lang="ts"> import { onActivated } from "vue&q ...

  9. [转帖]在 TiDB 中正确使用索引,性能提升 666 倍

    https://tidb.net/book/tidb-monthly/2022/2022-04/usercase/index-666 背景​ 最近在给一个物流系统做TiDB POC测试,这个系统是基于 ...

  10. [转帖]Sysbench - 一种系统性能benchmark

    SysBench是一款开源的.跨平台的.模块化的.多线程的性能测试工具,通过高负载地运行在数据库上,可以执行CPU/内存/线程/IO/数据库等方面的性能测试.用于评估操作系统的性能参数. 1 sysb ...