我想将字符串设置N=xxx为我的图的标题,其中xxx是我作为data参数传递的数据框中的观察数ggplot().在我当前的代码中,我第二次显式传递该数据帧作为sprintf()我在其中使用的参数labs():
ggplot(mtcars, aes(mpg, hp)) +
labs(title=sprintf("N=%i", nrow(mtcars))) +
geom_point()
Run Code Online (Sandbox Code Playgroud)
这确实产生了所需的标题,但它不适用于更复杂的任务:我使用dplyr管道来构建正在绘制的数据框,因为这是一个耗时的过程,我不想重复管道第二次获取行数,如示例中所示.
那么,如何ggplot()从用于修改绘图的函数的参数规范中访问作为参数传递的数据框?
当我尝试使用pandas plotting函数结合Seaborn的FacetGrids将我的数据帧绘制为带有时间索引的区域图时,我在Qt应用程序中遇到了问题.会发生什么是正确创建网格布局,但这些图不会显示在这些网格中.但是,使用Seaborn绘图功能可以正常工作.
我试图通过从我的其余代码中隔离绘图例程来弄清楚发生了什么,我发现了一个非常意外的行为,如下所示(使用ipython notebook):
%matplotlib inline
import pandas as pd
import seaborn as sns
df = pd.DataFrame({
"Home": [76, 64, 38, 78, 63, 45, 32, 46, 13, 40],
"Away": [55, 67, 70, 56, 59, 69, 72, 24, 45, 21],
"Team": ["T1"] * 5 + ["T2"] * 5,
"Year": ["1991", "1992", "1993", "1994", "1995"] * 2})
Run Code Online (Sandbox Code Playgroud)
现在,我想要做的是绘制两个方面,每个方面一个.每个方面都应将"离开"和"主页"列显示为两个单独的时间序列.根据另一个问题的建议(使用Seaborn FacetGrid绘制时间序列),我编写了一个函数,为map_dataframe()传递给它的子集调用pandas plotting函数:
def plot_area(data, color):
data[["Home", "Away"]].index = pd.to_datetime(data["Year"])
data[["Home", "Away"]].plot(kind="area")
Run Code Online (Sandbox Code Playgroud)
但是,当使用此函数时,结果是相当意外的:FacetGrid是正确创建和初始化的,但是对pandas方法的两次调用不会将此网格用作其绘图区域,而是出现在其他位置.
g = sns.FacetGrid(df, col="Team")
g.map_dataframe(plot_area)
Run Code Online (Sandbox Code Playgroud)
<seaborn.axisgrid.FacetGrid at 0x1a25110>
Run Code Online (Sandbox Code Playgroud)
输出屏幕截图:

在上面链接的帖子中,@ …
我正在写一个简单的字符串解析器,它允许类似regexp的量词.输入字符串可能如下所示:
s = "x y{1,2} z"
Run Code Online (Sandbox Code Playgroud)
我的解析器函数将此字符串转换为元组列表:
list_of_tuples = [("x", 1, 1), ("y", 1, 2), ("z", 1, 1)]
Run Code Online (Sandbox Code Playgroud)
现在,棘手的一点是我需要一个由量化指定的所有有效组合的列表.组合都必须具有相同数量的元素,并且该值None用于填充.对于给定的示例,预期输出为
[["x", "y", None, "z"], ["x", "y", "y", "z"]]
Run Code Online (Sandbox Code Playgroud)
我确实有一个可行的解决方案,但我对它并不满意:它使用两个嵌套for循环,我发现代码有点模糊,所以有一些尴尬和笨拙的事情:
import itertools
def permute_input(lot):
outer = []
# is there something that replaces these nested loops?
for val, start, end in lot:
inner = []
# For each tuple, create a list of constant length
# Each element contains a different number of
# repetitions of the value …Run Code Online (Sandbox Code Playgroud) 假设我使用第三方函数,magic_plot(data, ax)该函数根据提供的数据向轴添加补丁集合。让我们进一步假设我想更改已添加的特定补丁的颜色。我怎么做?
from numpy.random import rand
from matplotlib.collections import PatchCollection
from matplotlib.patches import Circle
import matplotlib.pyplot as plt
def magic_plot(data, ax):
"""
A third-party plotting function, not modifiable by end-users.
"""
lst = []
for i in range(10):
patch = Circle((rand(), rand()), rand())
lst.append(patch)
collection = PatchCollection(lst)
ax.add_collection(collection)
ax = plt.gca()
data = rand(100)
# create the plot:
magic_plot(data, ax)
# obtain the PatchCollection created by magic_plot():
collection = ax.collections[0]
Run Code Online (Sandbox Code Playgroud)
如上所示,我可以从 轴中检索集合ax.collections,但如何从这里继续?
我假设我需要访问存储在该对象中的补丁列表PatchCollection。但是,在对类似问题“matplotlib …