Matplotlib配置图例legend()设置透明和并排显示
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()设置透明和并排显示的更多相关文章
- 【转】matplotlib制图——图例legend
转自:https://www.cnblogs.com/alimin1987/p/8047833.html import matplotlib.pyplot as pltimport numpy as ...
- 【Python学习笔记】调整matplotlib的图例legend的位置
有时默认的图例位置不符合我们的需要,那么我们可以使用下面的代码对legend位置进行调整. plt.legend(loc='String or Number', bbox_to_anchor=(num ...
- gnuplot图例legend设置
//将图例放在右下角 set key bottom //将图例放在中间 set key center //将图例放在左边 set key left //将图例放在指定位置右下角的坐标为(10,0.7) ...
- matplotlib中的legend()—显示图例
源自 matplotlib中的legend()——用于显示图例 -- 博客园 http://www.cnblogs.com/yinheyi/p/6792120.html legend()的一个用法: ...
- matplotlib 进阶之Legend guide
目录 matplotlib.pyplot.legend 方法1自动检测 方法2为现有的Artist添加 方3显示添加图例 控制图例的输入 为一类Artist设置图例 Legend 的位置 loc, b ...
- 妙方之解决matplotlib的图例里的中文呈现小方形
妙方之解决matplotlib的图例里的中文呈现小方形 分析思路: 每个中文都对应地呈现一个小方形, 不多也不少. 不能说是乱码. 应该是matplotlib的默认字库不支持中文造成的. 应对办法: ...
- Android设置透明、半透明等效果
设置透明效果 大概有三种 1.用android系统的透明效果Java代码 android:background="@android:color/transparent" 例如 设 ...
- highcharts图表的图例legend怎么改变显示位置
一.将图例Legend放于图表右侧1.设置chart的marginRight属性值:chart: { marginRight: 120}2.设置legend图例属性值如下 legend: { alig ...
- Matplotlib绘图双纵坐标轴设置及控制设置时间格式
双y轴坐标轴图 今天利用matplotlib绘图,想要完成一个双坐标格式的图. fig=plt.figure(figsize=(20,15)) ax1=fig.add_subplot(111) ax1 ...
- highcharts图表的图例legend
一.将图例Legend放于图表右侧1.设置chart的marginRight属性值:chart: { marginRight: 120}2.设置legend图例属性值如下 legend: { alig ...
随机推荐
- 【每日一题】39. Contest(树状数组 / 容斥分治)
补题链接:Here 算法涉及:树状数组.CDQ分治 n支队伍一共参加了三场比赛. 一支队伍x认为自己比另一支队伍y强当且仅当x在至少一场比赛中比y的排名高. 求有多少组(x,y),使得x自己觉得比y强 ...
- iview+vue 加载进度条
效果:浏览器最上方出现一个进度条. main.js import Vue from 'vue' import ViewUI from 'view-design'; import router from ...
- centos7.9 安装oracle11g
安装环境: 操作系统:CentOS Linux release 7.9.2009 (Core)orcle安装包:linux.x64_11gR2_database_1of2.zip. linux.x64 ...
- python进阶(6)--类
文档目录: 一.创建类 二.使用类与实例 三.继承 ---------------------------------------分割线:正文----------------------------- ...
- 43 干货系列从零用Rust编写负载均衡及代理,内网穿透方案完整部署
wmproxy wmproxy已用Rust实现http/https代理, socks5代理, 反向代理, 静态文件服务器,四层TCP/UDP转发,七层负载均衡,内网穿透,后续将实现websocket代 ...
- 修改elasticsearch默认索引返回数
1. 背景 (1) 客户反映es查询只能返回10000个数据,而需求时返回1.9W个数据,因此需要设置对应索引的默认返回数index.max_result_window (2) 给客户部署的服务以do ...
- Git-分支-branch-checkout-merge
- SqlSugar DbContext
title: SqlSugar DbContext date: 2023-02-16 20:01:41 tags: SqlSugar categories: ORM description: 总结整理 ...
- [转帖]能使 Oracle 索引失效的六大限制条件
Oracle 索引的目标是避免全表扫描,提高查询效率,但有些时候却适得其反. 例如一张表中有上百万条数据,对某个字段加了索引,但是查询时性能并没有什么提高,这可能是 oracle 索引失效造成的.or ...
- [转帖]【SOP】最佳实践之 TiDB 业务写变慢分析
https://zhuanlan.zhihu.com/p/647831844 前言 在日常业务使用或运维管理 TiDB 的过程中,每个开发人员或数据库管理员都或多或少遇到过 SQL 变慢的问题.这类问 ...