小编023*_*5ev的帖子

Matplotlib:使用 twinx 时在图例上绘制数据

我正在尝试使用 Python 和 Matplotlib 来绘制许多不同的数据集。我正在使用 twinx 将一个数据集绘制在主轴上,另一个绘制在次轴上。我想为这些数据集提供两个单独的图例。

在我当前的解决方案中,来自辅助轴的数据绘制在主轴图例的顶部,而来自主轴的数据未绘制在辅助轴图例上。

我根据此处的示例生成了一个简化版本:http : //matplotlib.org/users/legend_guide.html

这是我到目前为止所拥有的:

import matplotlib.pyplot as plt
import pylab

fig, ax1 = plt.subplots()
fig.set_size_inches(18/1.5, 10/1.5)
ax2 = ax1.twinx()

ax1.plot([1,2,3], label="Line 1", linestyle='--')
ax2.plot([3,2,1], label="Line 2", linewidth=4)

ax1.legend(loc=2, borderaxespad=1.)
ax2.legend(loc=1, borderaxespad=1.)

pylab.savefig('test.png',bbox_inches='tight', dpi=300, facecolor='w', edgecolor='k')
Run Code Online (Sandbox Code Playgroud)

结果如下图: 数字

如图所示,来自 ax2 的数据绘制在 ax1 图例上,我希望图例位于数据顶部。我在这里缺少什么?

谢谢您的帮助。

python plot matplotlib

6
推荐指数
2
解决办法
4323
查看次数

Python正则表达式:除外部大括号外,搜索并替换字符

我正在研究Python中的一个问题,我需要在字符串中的任何地方搜索和替换某个字符,除非它位于花括号之间.当角色位于大括号之间时,我知道如何执行此操作,但是当它位于大括号外时不知道如何执行此操作.基本上,我希望搜索跳过两个分隔符之间的任何内容.

我目前的工作是在整个字符串上执行搜索和替换,然后再次搜索并替换大括号以撤消最后一次替换的那部分.

以下是我正在寻找的功能的示例:

import re
>>> str = 'I have a _cat, here is a pic {cat_pic}. Another_pic {cat_figure}'
>>> re.sub(regex1,'/_',str)
'I have a /_cat, here is a pic {cat_pic}. Another/_pic {cat_figure}'
Run Code Online (Sandbox Code Playgroud)

我目前使用的解决方案分为两个步骤:

import re
>>> str = 'I have a _cat, here is a pic {cat_pic}. Another_pic {cat_figure}'
>>> s1 = re.sub('_','/_',str)
>>> s1
'I have a /_cat, here is a pic {cat/_pic}. Another/_pic {cat/_figure}'
>>> s2 = re.sub(r'\{(.+?)/_(.+?)\}', r'{\1_\2}', s1)
>>> s2
'I have a /_cat, here is …
Run Code Online (Sandbox Code Playgroud)

python regex string

1
推荐指数
1
解决办法
435
查看次数

标签 统计

python ×2

matplotlib ×1

plot ×1

regex ×1

string ×1