- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
考虑以下 pandas 系列 s
和 plot
import pandas as pd
import numpy as np
s = pd.Series(np.random.lognormal(.001, .01, 100))
ax = s.cumprod().plot()
ax.set_title('My Log Normal Example', position=(.5, 1.02),
backgroundcolor='black', color='white')
如何让包含标题的框横跨整个图?
最佳答案
当然可以获取标题的边界框,它是一个 Text
元素。这可以通过
title = ax.set_title(...)
bb = title.get_bbox_patch()
原则上,然后可以操纵边界框,例如通过bb.set_width(...)
。但是,一旦 matplotlib 将标题绘制到 Canvas 上,所有设置都会丢失。至少我是这样解释 Text
的 draw()
方法的。
我不知道设置边界框的其他方法。例如,legend
的边界框可以通过
设置plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, mode="expand")
使其在整个轴范围内扩展(参见 here ).如果 Text
也有相同的选项,那将非常有用。但就目前而言,我们没有。
Text
对象允许设置 bbox
通常用于设置边界框样式的参数。无法设置边界框范围,但它接受周围框的一些属性字典。接受的属性之一是 boxstyle
.默认情况下,这是一个正方形
,但可以设置为圆形或箭头或其他奇怪的形状。
那些 boxstyle
实际上是可能解决方案的关键。它们都继承自 BoxStyle._Base
并且 - 如 the bottom of the annotations guide 所示- 可以定义自定义形状,继承 BoxStyle._Base
。
以下解决方案基于 BoxStyle._Base
的子类化,它接受轴的宽度作为参数并绘制标题的矩形路径,使其恰好具有此宽度.
作为奖励,我们可以注册一个事件处理程序,以便此宽度一旦因调整窗口大小而发生变化,就会进行调整。
代码如下:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from matplotlib.path import Path
from matplotlib.patches import BoxStyle
class ExtendedTextBox(BoxStyle._Base):
"""
An Extended Text Box that expands to the axes limits
if set in the middle of the axes
"""
def __init__(self, pad=0.3, width=500.):
"""
width:
width of the textbox.
Use `ax.get_window_extent().width`
to get the width of the axes.
pad:
amount of padding (in vertical direction only)
"""
self.width=width
self.pad = pad
super(ExtendedTextBox, self).__init__()
def transmute(self, x0, y0, width, height, mutation_size):
"""
x0 and y0 are the lower left corner of original text box
They are set automatically by matplotlib
"""
# padding
pad = mutation_size * self.pad
# we add the padding only to the box height
height = height + 2.*pad
# boundary of the padded box
y0 = y0 - pad
y1 = y0 + height
_x0 = x0
x0 = _x0 +width /2. - self.width/2.
x1 = _x0 +width /2. + self.width/2.
cp = [(x0, y0),
(x1, y0), (x1, y1), (x0, y1),
(x0, y0)]
com = [Path.MOVETO,
Path.LINETO, Path.LINETO, Path.LINETO,
Path.CLOSEPOLY]
path = Path(cp, com)
return path
dpi = 80
# register the custom style
BoxStyle._style_list["ext"] = ExtendedTextBox
plt.figure(dpi=dpi)
s = pd.Series(np.random.lognormal(.001, .01, 100))
ax = s.cumprod().plot()
# set the title position to the horizontal center (0.5) of the axes
title = ax.set_title('My Log Normal Example', position=(.5, 1.02),
backgroundcolor='black', color='white')
# set the box style of the title text box toour custom box
bb = title.get_bbox_patch()
# use the axes' width as width of the text box
bb.set_boxstyle("ext", pad=0.4, width=ax.get_window_extent().width )
# Optionally: use eventhandler to resize the title box, in case the window is resized
def on_resize(event):
print "resize"
bb.set_boxstyle("ext", pad=0.4, width=ax.get_window_extent().width )
cid = plt.gcf().canvas.mpl_connect('resize_event', on_resize)
# use the same dpi for saving to file as for plotting on screen
plt.savefig(__file__+".png", dpi=dpi)
plt.show()
如果有人对更轻的解决方案感兴趣,还可以选择使用标题边界框的 mutation_aspect
,在绘制标题时显然保持不变。虽然 mutation_aspect
本身基本上只改变框的高度,但可以为框使用非常大的填充并将 mutation_aspect
设置为非常小的数字,这样在最后框出现在宽度扩展。这个解决方案的明显缺点是,填充和纵横比的值必须通过反复试验找到,并且会随着不同的字体和图形大小而变化。在我的例子中,mutation_aspect = 0.04
和 pad=11.9
的值产生了期望的结果,但在其他系统上,它们当然可能不同。
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
s = pd.Series(np.random.lognormal(.001, .01, 100))
ax = s.cumprod().plot()
title = ax.set_title('My Log Normal Example', position=(.5, 1.02),
backgroundcolor='black', color='white',
verticalalignment="bottom", horizontalalignment="center")
title._bbox_patch._mutation_aspect = 0.04
title.get_bbox_patch().set_boxstyle("square", pad=11.9)
plt.tight_layout()
plt.savefig(__file__+".png")
plt.show()
关于python - 如何使标题框的宽度跨越整个绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40796117/
我附上了一个我尝试使用 html/css 实现的示例(如果您看不到图像:名字和姓氏,然后第二行是职位描述)。我希望所有文本(两行)在一个 div 中强制对齐(左和右),但我不确定这是否可能。我尝试了一
我想使两个 h1 元素成为 div 上的标题/页眉。所以每个都在特定的 div 之上。 Youtube Achievements
我想让每个 EditText 对象都有自己的标题,就像 Pure Android 指南中那样 (screenshot) 这个东西有原生支持吗?我想他们也可能会使用带有部分的 ListView ,但这对
是否可以像 UITableView headerView 一样创建 UICollectionView 标题 View ?我的意思是整个集合 View 的标题 View ,而不是每个部分的重复 View
我一直在遵循有关排版的 Google 官方 Material 设计指南 (http://www.google.com/design/spec/style/typography.html),但我发现它们
我目前正在尝试找到可以帮助我从视频文件中提取元数据或信息的 python 库,例如 [ mp4, Mkv, Avi, WebM, mpg ] 格式为例。 我主要从视频文件中提取的主要数据是 [标题、描
你好, 这是我正在尝试做的: 将每个缩略图的内容(img + 标题)居中。我的 img 必须是 span3,标题必须是 span4。 这是我的问题: 我可以获取内容中心,或者标题 float 在 im
我有一个带有导航栏的应用程序,可以从一个 View Controller 导航到下一个 View Controller 。在某些模拟器和设备上导航到下一个 View Controller 时,后退按钮
我遇到了一些非常酷的 t-sql,可以从一个 t-sql 查询中的选定行生成一个逗号分隔的列值列表: SELECT @MyList = ISNULL(@MyList,'') + Title + ',
请确保将 HTML heading 标签只用于标题。不要仅仅是为了生成粗体或的文本而使用标题。 搜索引擎使用标题为您的网页的结构和内容编制索引。 因为用户可以通过标题来快速浏览您的网页,所以用标
我正在使用 wkhtmltopdf 将 html 转换为 pdf。 我想在每个页面中添加标题,但它只显示在第一页(目录)中。 我使用的命令是 "C:\Program Files\wkhtmltopdf
如何使用 ggplot2 显示观察的方向(标题)?有没有办法调整shape=17 (三角形)以便它“指向”下一次观察? 示例代码 library(ggplot2) dat % pivot_wide
我尝试在 cocoa 应用程序中显示/隐藏标题栏。我使用以下代码: if ([window styleMask]==NSResizableWindowMask) { [wind
我有这样的 HTML 标题 http://s1.postimg.org/4ebyk3qwv/image.png 当我编写这段代码时: document.getElementById("TL85_1_
我叫麦克。谢谢你的帮助。 在Wordpress中,我们已经设计了我们的网站,以便在Facebook调试器中og数据尽可能接近youtube。尽管如此,在Facebook上共享视频的方式还是不同的。尽管
从 web 应用程序的客户端,我点击了服务器端路由,它只是第三方 API 的包装器。使用分派(dispatch),我试图让服务器端请求返回 exact header 和第三方 API 对客户端 AJA
从 web 应用程序的客户端,我点击了服务器端路由,它只是第三方 API 的包装器。使用分派(dispatch),我试图让服务器端请求返回 exact header 和第三方 API 对客户端 AJA
我是 SAPUI5 的新手,在导航、侧边栏和标题方面遇到一些问题。我想开发一个带有标题和侧边栏的应用程序。我为此使用“ToolPage”。每个页面都包含工具页,如下所示:
我最近在为客户做的项目中被介绍给Go。他们已经建立了代码库,需要进行一些更改。 我注意到所有的方法,结构等等都有一些奇怪的类似于标题的注释,如下所示: // SomeType ... type Som
我创建了一个采用整个屏幕布局的标题布局(xml 文件)... 我还创建了一个 listView 并将此 header_layout 添加到 listView 中: LayoutInflater inf
我是一名优秀的程序员,十分优秀!