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

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

  1. python绘制多个条形图_python – 在Matplotlib中绘制多个直方图 – 颜色或并排条形图...

    问题:在Matplotlib中绘制多个直方图时,我无法区分绘图与另一个绘图 图像问题:** **次要问题:部分左侧标签"计数"不在图像范围内.为什么? 描述 我想绘制3个不同组的直 ...

  2. python的logo的代码_Python使用Matplotlib实现Logos设计代码

    本文主要展示了使用matplotlib设计logo的示例及完整代码,首先看下其演示结果: Python代码如下: import numpy as np import matplotlib as mpl ...

  3. python制作3d相册代码_Python使用matplotlib绘制3D图形(代码示例)

    本篇文章给大家带来的内容是关于Python使用matplotlib绘制3D图形(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 3D图形在数据分析.数据建模.图形和图像处理 ...

  4. python怎么编写对称图案_python – 无论matplotlib中的箭头角度如何,都使箭头形状对称...

    以下代码生成下图.箭头的形状取决于箭头的角度.如何使所有箭头都具有相同的对称形状?我特别要求 Python 3,但欢迎一般的解决方案. import matplotlib.pyplot as plt ...

  5. python画两条曲线_python – 在Matplotlib中绘制两个图之间的线

    在许多情况下,来自其他答案的解决方案是次优的(因为只有在计算点之后没有对图进行任何更改时它们才有效). 更好的解决方案是使用专门设计的ConnectionPatch: import matplotli ...

  6. python怎么弄成白色背景_python – 在matplotlib中为colorbar添加白色背景

    这是创建背景的解决方案: import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.axes_grid1.inset_l ...

  7. python怎么变各种颜色_python – 在matplotlib中变暗或变亮颜色

    这是一个来自 my gist的函数,用于减轻我认为可以使用matplotlib已知的任何颜色格式的任何颜色.我认为设定金额> 1也可能变暗. def lighten_color(color, a ...

  8. python使用matplotlib 画柱状图代码_Python 使用 matplotlib 画柱状图教程

    Python 使用 matplotlib 画图是非常方便的,之前的文章记录了<Python 使用 matplotlib 画折线图教程>,今天就再次记录一下使用 matplotlib 画柱状 ...

  9. python换循环颜色_python – 增加matplotlib颜色循环

    你可以打电话 ax2._get_lines.get_next_color() 在彩色上推进彩色循环仪.不幸的是,这会访问私有属性._get_lines,因此这不是官方公共API的一部分,并且不能保证在 ...

  10. python动态心形代码_Python实现酷炫的动态交互式数据可视化,附代码!

    (关注公众号AI新视野,发送'资料'二字,免费获取50G人工智能视频教程!) 本文介绍如何创建交互式图表和小组件,使用python做数据可视化.涉及的python库有:Plotly, Bokeh, n ...

最新文章

  1. 【大话设计模式】——浅谈设计模式基础
  2. 问题 | 解决Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll 问题(pycharm+Tensorflow)
  3. css --- 选择器
  4. PWN-PRACTICE-BUUCTF-21
  5. 深度学习(六十四)Faster R-CNN物体检测
  6. 总结|数学建模的收获
  7. DP动态规划--m处理器问题- m processors(FZU - 1442)
  8. 字体:等宽字体与比例字体 - Monospaced font Proportional font
  9. android 图片编辑工具,照片编辑器:Photo Editor
  10. Katana:1 PGP Workthrought
  11. 虚拟机克隆的服务器怎么改mac地址,Centos6克隆虚拟机改IP和mac地址
  12. Matplotlib中画图,使用带有边框的条线
  13. 熤星传媒:抖音几万粉丝能入驻星图?
  14. ae形状图层怎样合并路径?
  15. Schnorr签名体制
  16. 基于Winnow的中文邮件分类器的设计
  17. iconfont的基本使用
  18. Android Studio精彩案例(七)《ToolBar使用详解一》
  19. 亲子沟通技巧学前教育培训.pptx
  20. ssm+jsp计算机毕业设计壹家吃货店网站quk1f(程序+lw+源码+远程部署)

热门文章

  1. Android Binder传递文件描述符原理分析
  2. 计算机注销操作,电脑注销快捷键
  3. php 公众号推送图片尺寸,微信公众号发图文消息图片的尺寸是多少为好?
  4. flink on yarn ——报错ResourceLocalizationService: Could not carry out resource dir checks
  5. Android NDK墓碑/崩溃分析
  6. 树的计数 + prufer序列与Cayley公式 学习笔记
  7. 网易互娱2020-9月22日笔试题记录
  8. 数据库文档自动生成工具
  9. 8K V-by-One LVDS信号发生器
  10. 统计字符数 —— C++