当前位置: 首页 > news >正文

python加粗线宽代码_python-增加matplotlib中图例行的线宽

python-增加matplotlib中图例行的线宽

我知道,如果更改线的线宽,则会在图例中自动更新。但是,我只想更改图例的线宽而不影响绘图。

4个解决方案

62 votes

这是一个简单的例子:

import numpy as np

import matplotlib.pyplot as plt

# make some data

x = np.linspace(0, 2*np.pi)

y1 = np.sin(x)

y2 = np.cos(x)

# plot sin(x) and cos(x)

p1 = plt.plot(x, y1, 'b-', linewidth=1.0)

p2 = plt.plot(x, y2, 'r-', linewidth=1.0)

# make a legend for both plots

leg = plt.legend([p1, p2], ['sin(x)', 'cos(x)'], loc=1)

# set the linewidth of each legend object

for legobj in leg.legendHandles:

legobj.set_linewidth(2.0)

plt.show()

Brendan Wood answered 2020-02-09T02:58:01Z

6 votes

@Brendan Wood的方法使用pyplot提供的api。在matplotlib中,首选使用轴的面向对象样式。 下面是如何使用axes方法实现此目的的方法。

import numpy as np

import matplotlib.pyplot as plt

# make some data

x = np.linspace(0, 2*np.pi)

y1 = np.sin(x)

y2 = np.cos(x)

fig, ax = plt.subplots()

ax.plot(x, y1, linewidth=1.0, label='sin(x)')

ax.plot(x, y2, linewidth=1.0, label='cos(x)')

leg = ax.legend()

for line in leg.get_lines():

line.set_linewidth(4.0)

plt.show()

生成的图如下所示,

jdhao answered 2020-02-09T02:58:28Z

3 votes

如果要更改绘图中的所有线条,定义自己的图例处理程序可能会很有用:

import matplotlib.pyplot as plt

from matplotlib import legend_handler

from matplotlib.lines import Line2D

import numpy as np

class MyHandlerLine2D(legend_handler.HandlerLine2D):

def create_artists(self, legend, orig_handle,

xdescent, ydescent, width, height, fontsize,

trans):

xdata, xdata_marker = self.get_xdata(legend, xdescent, ydescent,

width, height, fontsize)

ydata = ((height-ydescent)/2.)*np.ones(xdata.shape, float)

legline = Line2D(xdata, ydata)

self.update_prop(legline, orig_handle, legend)

#legline.update_from(orig_handle)

#legend._set_artist_props(legline) # after update

#legline.set_clip_box(None)

#legline.set_clip_path(None)

legline.set_drawstyle('default')

legline.set_marker("")

legline.set_linewidth(10)

legline_marker = Line2D(xdata_marker, ydata[:len(xdata_marker)])

self.update_prop(legline_marker, orig_handle, legend)

#legline_marker.update_from(orig_handle)

#legend._set_artist_props(legline_marker)

#legline_marker.set_clip_box(None)

#legline_marker.set_clip_path(None)

legline_marker.set_linestyle('None')

if legend.markerscale != 1:

newsz = legline_marker.get_markersize()*legend.markerscale

legline_marker.set_markersize(newsz)

# we don't want to add this to the return list because

# the texts and handles are assumed to be in one-to-one

# correpondence.

legline._legmarker = legline_marker

return [legline, legline_marker]

plt.plot( [0, 1], [0, 1], '-r', lw=1, label='Line' )

plt.legend(handler_map={Line2D:MyHandlerLine2D()})

plt.show()

David Zwicker answered 2020-02-09T02:58:48Z

3 votes

默认情况下,图例包含行本身。 因此,更改画布中线条的线宽也会更改图例中的线条(反之亦然,因为它们本质上是同一对象)。

一种可能的解决方案是使用画布上艺术家的副本,并仅更改副本的线宽。

import numpy as np

import matplotlib.pyplot as plt

import copy

x = np.linspace(0, 2*np.pi)

y1 = np.sin(x)

y2 = np.cos(x)

fig = plt.figure()

ax = fig.add_subplot(111)

ax.plot(x, y1, c='b', label='y1',linewidth=1.0)

ax.plot(x, y2, c='r', label='y2')

# obtain the handles and labels from the figure

handles, labels = ax.get_legend_handles_labels()

# copy the handles

handles = [copy.copy(ha) for ha in handles ]

# set the linewidths to the copies

[ha.set_linewidth(7) for ha in handles ]

# put the copies into the legend

leg = plt.legend(handles=handles, labels=labels)

plt.savefig('leg_example')

plt.show()

另一种选择是使用handler_map和更新功能。 这以某种方式是自动的,指定处理程序映射将自动使图例中的任何行宽7点。

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.legend_handler import HandlerLine2D

x = np.linspace(0, 2*np.pi)

y1 = np.sin(x)

y2 = np.cos(x)

fig = plt.figure()

ax = fig.add_subplot(111)

ax.plot(x, y1, c='b', label='y1',linewidth=1.0)

ax.plot(x, y2, c='r', label='y2')

linewidth=7

def update(handle, orig):

handle.update_from(orig)

handle.set_linewidth(7)

plt.legend(handler_map={plt.Line2D : HandlerLine2D(update_func=update)})

plt.show()

结果与上面相同。

ImportanceOfBeingErnest answered 2020-02-09T02:59:22Z

相关文章:

  • VSTS for Testers学习笔记目录
  • nginx 80端口转发失效_搞懂Nginx
  • python04-0.1_Python04(基础语法)
  • MSSQL 中 Stuff 的应用
  • 最大独立匹配_射频电路设计中阻抗匹配的必要性
  • SQL日期推算...
  • python强制关闭线程_在python中实现强制关闭线程的示例
  • ABAP將數字輸出前面補0
  • potplayer哪个版本最好用_市面上有多少云手机?哪个最好用?
  • ABAP两个非法修改系统程式的方法...
  • python作者 es6_Es6 写的文件import 起来解决方案详解
  • route命令详解_程序员必备的学习笔记《TCP/IP详解》IP选路、动态选路、UDP 协议...
  • 5分钟让你整明白美国金融危机爆发的原因
  • python中字典copy_python中字典、元祖、浅拷贝、深拷贝
  • javascript在IE和Firefox中的兼容考虑
  • hexo+github搭建个人博客
  • 【译】理解JavaScript:new 关键字
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • Android路由框架AnnoRouter:使用Java接口来定义路由跳转
  • C学习-枚举(九)
  • Docker 1.12实践:Docker Service、Stack与分布式应用捆绑包
  • flutter的key在widget list的作用以及必要性
  • Git同步原始仓库到Fork仓库中
  • Java 内存分配及垃圾回收机制初探
  • Java应用性能调优
  • php ci框架整合银盛支付
  • React-redux的原理以及使用
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • SegmentFault 2015 Top Rank
  • 第13期 DApp 榜单 :来,吃我这波安利
  • 关于List、List?、ListObject的区别
  • 计算机常识 - 收藏集 - 掘金
  • 七牛云 DV OV EV SSL 证书上线,限时折扣低至 6.75 折!
  • 前端
  • 实战:基于Spring Boot快速开发RESTful风格API接口
  • 线性表及其算法(java实现)
  • 项目实战-Api的解决方案
  • Spring Batch JSON 支持
  • 函数计算新功能-----支持C#函数
  • 回归生活:清理微信公众号
  • 新年再起“裁员潮”,“钢铁侠”马斯克要一举裁掉SpaceX 600余名员工 ...
  • (10)STL算法之搜索(二) 二分查找
  • (3)llvm ir转换过程
  • (3)STL算法之搜索
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (附源码)spring boot建达集团公司平台 毕业设计 141538
  • (附源码)springboot教学评价 毕业设计 641310
  • (附源码)ssm捐赠救助系统 毕业设计 060945
  • (三十五)大数据实战——Superset可视化平台搭建
  • (一)u-boot-nand.bin的下载
  • (转)微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)...
  • (转载)从 Java 代码到 Java 堆
  • .Net CoreRabbitMQ消息存储可靠机制
  • .NET Micro Framework初体验