Python 缩放比例变化时的绘图显示错误:';第1帧图的填充比例与第一帧图的填充比例有不同的限制;

Python 缩放比例变化时的绘图显示错误:';第1帧图的填充比例与第一帧图的填充比例有不同的限制;,python,r,animation,gganimate,plotnine,Python,R,Animation,Gganimate,Plotnine,我试图使用gganimate(R)等价物plotnine(python)。当其中一个绘图引入新的美学比例类别时,我遇到了问题,导致控制台抛出错误:第1帧绘图的填充比例与第一帧的填充比例有不同的限制。 这似乎是由于必须生成所有绘图的生成器,然后将它们粘在一起,如plotnine文档所示()。当我使用gganimate()时,库足够聪明,可以检查将调用的所有比例,并相应地打印完整的比例 我尝试使用scale\u fill\u manual() 我正在plotnine中寻找解决这个问题的方法(如果有更

我试图使用
gganimate
(R)等价物
plotnine
(python)。当其中一个绘图引入新的美学比例类别时,我遇到了问题,导致控制台抛出错误:
第1帧绘图的填充比例与第一帧的填充比例有不同的限制。

这似乎是由于必须生成所有绘图的生成器,然后将它们粘在一起,如plotnine文档所示()。当我使用
gganimate()
时,库足够聪明,可以检查将调用的所有比例,并相应地打印完整的比例

我尝试使用
scale\u fill\u manual()

我正在plotnine中寻找解决这个问题的方法(如果有更好的python动画模块的方法,如果有建议,我可以学习)

以下是一个成功的例子:

在plotnine中首次尝试:

#import modules
import pandas as pd
from plotnine import *
from plotnine.animation import PlotnineAnimation
#create the dataframe
df = pd.DataFrame({'run': [1, 1, 1, 2, 2, 2], 
             'x_pos': [1, 2, 3, 2, 3, 4], 
             'y_pos': [4, 5, 6, 5, 6, 7], 
             'status': ['A', 'B', 'A', 'A', 'B', 'C'] })

#write a function that creates all the plots
def plot(x):
    df2 = df[df['run'] == x]

    p = (ggplot(df2,
               aes(x = 'x_pos', y = 'y_pos', fill = 'status'))
         + geom_point()
    )
    return(p)

#save the plots as a generator as per the plotnine docs (https://plotnine.readthedocs.io/en/stable/generated/plotnine.animation.PlotnineAnimation.html)
plots = (plot(i) for i in range(1, 3))

#create the animation
animation = PlotnineAnimation(plots, interval=100, repeat_delay=500)
引发错误:
PlotnineError:“第1帧的打印填充比例与第一帧的填充比例具有不同的限制。”

如果我在R中使用
gganimate()
,一切正常-我从一开始就得到所有三种颜色比例:

library('ggplot2')
library('gganimate')

df <- data.frame(run = c(1, 1, 1, 2, 2, 2), 
                 x_pos = c(1, 2, 3, 2, 3, 4), 
                 y_pos = c(4, 5, 6, 5, 6, 7), 
                 status = c('A', 'B', 'A', 'A', 'B', 'C'))

ggplot(df, aes(x = x_pos, y = y_pos, col = status)) + 
  geom_point() + 
  transition_states(run)
尽管在
scale\u color\u手册()

然后在Python中:

#Filter the data frame created above
df2 = df[df['run'] == 1]

#Create the plot - stating scale_fill_manual()
p = (ggplot(df2,
        aes(x = 'x_pos', y = 'y_pos', fill = 'status'))
 + geom_point()
 + scale_fill_manual(labels = ['A', 'B', 'C'], 
                    values = ['red', 'blue', 'green'])
)

#Print the plot
print(p)
抛出关于数组长度错误的错误(
ValueError:数组必须全部长度相同
),这很有意义:我要求的值和颜色比筛选的数据集中的值和颜色更多。这意味着我不能让第一帧匹配第二帧,这是我试图解决的错误


有人知道如何使用
plotnine
,就像它在
gganimate()
中那样工作吗?此外(虽然不太重要),关于为什么
plotnine
geom_point()
填充,而
ggplot2
geom_point()

在python中,您应该将
状态设置为分类,最好是具体设置
x
y
美学的限制,如果由绘图系统决定,可能会出错

在Plotnine中,
geom_point
的所有关键打印形状都有内部区域和周围的笔划,因此您可以使用
fill
color
以其中一个为目标。但这是一个非常复杂的细节,很可能会让用户感到沮丧,因此当您映射到
颜色而不是
填充时,它会为两者设置相同的颜色。在两者之间,Plotnine无法识别缩写
col

将熊猫作为pd导入
从plotnine导入*
从plotnine.animation导入PlotnineAnimation
#创建数据帧
df=pd.DataFrame({'run':[1,1,1,2,2,2],
“x_pos”:[1,2,3,2,3,4],
“y_pos”:[4,5,6,5,6,7],
‘状态’:[‘A’、‘B’、‘A’、‘A’、‘B’、‘C’]})
#分类确保每个子数据帧
#可用于创建具有正确限制的比例
df['status']=pd.Categorical(df['status'])
#编写一个创建所有绘图的函数
def绘图(x):
df2=df[df['run']==x]
p=(ggplot(df2,
aes(x='x_pos',y='y_pos',颜色='status'))
+几何点()
#指定x和y美学的限制
+比例x连续(极限=(df.x位置最小(),df.x位置最大())
+连续缩放(限制=(df.y位置最小(),df.y位置最大())
+主题(子情节调整={'right':0.85})#为图例留出空间
)
返回(p)
图=(范围(1,3)内的i的图(i))
#创建动画
动画=绘图动画(绘图,间隔=1000,重复\u延迟=500)
动画

很抱歉回复太慢。这个项目从我的盘子里掉了下来。这很好用。非常感谢。
#Filter the data frame created above
df2 = df[df['run'] == 1]

#Create the plot - stating scale_fill_manual()
p = (ggplot(df2,
        aes(x = 'x_pos', y = 'y_pos', fill = 'status'))
 + geom_point()
 + scale_fill_manual(labels = ['A', 'B', 'C'], 
                    values = ['red', 'blue', 'green'])
)

#Print the plot
print(p)