[英]TypeError: __init__() got multiple values for argument 'data'
I am trying to build a plot using ggplot(aes(x="order_date", y="total_price"), data=data_) and this is the code:我正在尝试使用 ggplot(aes(x="order_date", y="total_price"), data=data_) 构建 plot,这是代码:
# Core Python Data Analysis
from numpy.core.defchararray import index
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Plotting
from plotnine import (
ggplot, aes,
geom_col, geom_line, geom_smooth,
facet_wrap,
scale_y_continuous, scale_x_datetime,
labs,
theme, theme_minimal, theme_matplotlib,
expand_limits,
element_text
)
sales_by_months = df[[ 'order_date', 'total_price' ]] \
.set_index('order_date') \
.resample(rule='MS') \
.aggregate(np.sum) \
.reset_index()
data_ = sales_by_months
ggplot(aes(x="order_date", y="total_price"), data=data_)
I install all the libraries prior to this code.我在此代码之前安装了所有库。 I get this error:
我收到此错误:
TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_28504\2472174790.py in <module>
----> 1 ggplot(aes(x="order_date", y="total_price"), data=data_)
TypeError: __init__() got multiple values for argument 'data'
Can someone please hep me to solve it or show me why am I having this bug?有人可以帮我解决它或告诉我为什么我有这个错误吗?
WINDOW11, JUPYTER NOTEBOOK, PYTHON3
WINDOW11、JUPYTER 笔记本、Python3
The first argument you're passing into ggplot
is data
by default (because it's in first place in the source code, and you didn't provide any keyword), but then you pass another data
argument later (with keyword).默认情况下,您传递给
ggplot
的第一个参数是data
(因为它在源代码中排在首位,并且您没有提供任何关键字),但是稍后您会传递另一个data
参数(使用关键字)。 This is why such an error happens.这就是发生这种错误的原因。 Try to specify the keyword for the first argument, which is
mapping
.尝试为第一个参数指定关键字,即
mapping
。
Also, another tip which is not necessary but can help to improve readability and have a code more straight: put the data
argument first, then mapping
in second.此外,另一个技巧不是必需的,但可以帮助提高可读性并使代码更直接:将
data
参数放在第一位,然后mapping
放在第二位。
So you would have: ggplot(data=data_, mapping=aes(x="order_date", y="total_price"))
所以你会有:
ggplot(data=data_, mapping=aes(x="order_date", y="total_price"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.