ValueError: x must be 1D
ValueError: x must be 1D
我正在尝试为每一列创建一个饼图并在两个用户之间进行比较,当我 运行 我的代码出现错误时:
ValueError: x must be 1D
我不确定它的真正含义,我试图查找它但找不到任何有用的信息,关于如何解决此问题的任何指导。
谢谢!!!!
# Aggregate dataframe
summarized_df = df.groupby(['user']).agg({ 'favorite_counts': 'sum', # Tweets overview
'retweet_counts': 'sum',
'is_positive': 'sum',
'is_negative': 'sum',
'tweets_length': 'sum', # Tweets writing style
'tweets_uppercase': 'sum',
'tweets_punctuations': 'sum',
'tweets_questionmark': 'sum',
'is_norp': 'sum', # Tweets detailed writing style
'is_time': 'sum',
'is_org': 'sum',
'is_gpe': 'sum',
'is_loc': 'sum',
'is_product': 'sum',
'is_workart': 'sum',
'is_fac': 'sum',
'is_noun': 'sum',
'is_pron': 'sum',
'is_adv': 'sum',
'is_propn': 'sum',
'is_verb': 'sum',
'is_intj': 'sum' }).reset_index()
# Seperate dataframe
Tim_summarized_df = summarized_df[summarized_df.user == 'Timothy Caulfield'].drop('user', axis = 1).copy()
Keran_summarized_df = summarized_df[summarized_df.user == 'Dr. Karen James'].drop('user', axis = 1).copy()
# Get columns
summarized_cols = Tim_summarized_df.columns
# Create features for each round in list format
round_1_cols = summarized_cols[:5]
round_2_cols = summarized_cols[5:10]
round_3_cols = summarized_cols[10:15]
round_4_cols = summarized_cols[15:20]
round_5_cols = summarized_cols[20:22]
# Combine all lists into one list
temp_list = [round_1_cols, round_2_cols, round_3_cols, round_4_cols, round_5_cols]
# Dictionary to rename the titles
rep_title_dict = { 'favorite_counts': 'Tweets Likes',
'retweet_counts': 'Re-Tweets',
'is_positive': 'Positivity',
'is_negative': 'Negativity',
'tweets_length': 'Length of Tweets',
'tweets_uppercase': 'Uppercase Characters Used',
'tweets_punctuations': 'Punctuations Used',
'tweets_questionmark': 'Questionmark Used',
'is_norp': 'Nationalities | Religious | Political Groups',
'is_time': 'Mentioned Time Related',
'is_org': 'Corporate | Governmental',
'is_gpe': 'Countries | Cities | States',
'is_loc': 'Location Mentioned',
'is_product': 'Objects | Vehicles | Foods',
'is_workart': 'Books | Songs',
'is_fac': 'Buildings | Airports | Highways',
'is_noun': 'Noun Used',
'is_pron': 'Pronoun Used',
'is_adv': 'Adverb Used',
'is_propn': 'Propn (like Apple, UK, US)',
'is_verb': 'Verb Used',
'is_intj': 'Bravo | Hello | Ouch' }
# Create function to plot summarized details
def summarized_donut_plot(data_1, data_2, indx_list):#, plot_title):
if indx_list == 4:
fig, ax = plt.subplots(ncols = 2, figsize= (13,6))
else:
fig, ax = plt.subplots(ncols = 5, figsize= (35,6))
for indx, target in enumerate(temp_list[indx_list]):
total = df[target].sum()
# Data preproccessing
x = round(float(data_1[target] / total * 100), 1)
y = round(float(data_2[target] / total * 100), 1)
#print(total, x, y)
user_list = ['Timothy', 'Karen']
results_list = [x, y]
x = pd.DataFrame(data = results_list, index = user_list)
ax[indx].pie(x, autopct='%1.1f%%', textprops = {'fontsize': 10, 'color': 'w', 'weight': 'bold'})
ax[indx].add_patch(plt.Circle((0,0), 0.35, fc = 'white'))
ax[indx].legend(user_list, loc = 2)
ax[indx].set_title(rep_title_dict[target], size = 10)
plt.show()
for indx, sublist in enumerate(temp_list):
summarized_donut_plot( data_1 = Tim_summarized_df,
data_2 = Keran_summarized_df,
indx_list = indx )
以下是我得到的错误。
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [170], in <cell line: 1>()
1 for indx, sublist in enumerate(temp_list):
----> 2 summarized_donut_plot( data_1 = Tim_summarized_df,
3 data_2 = Keran_summarized_df,
4 indx_list = indx )
Input In [169], in summarized_donut_plot(data_1, data_2, indx_list)
86 results_list = [x, y]
88 x = pd.DataFrame(data = results_list, index = user_list)
---> 90 ax[indx].pie(x, autopct='%1.1f%%', textprops = {'fontsize': 10, 'color': 'w', 'weight': 'bold'})
91 ax[indx].add_patch(plt.Circle((0,0), 0.35, fc = 'white'))
93 ax[indx].legend(user_list, loc = 2)
File ~/.local/lib/python3.8/site-packages/matplotlib/__init__.py:1412, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs) 1409 @functools.wraps(func) 1410 def inner(ax, *args, data=None,
**kwargs): 1411 if data is None:
-> 1412 return func(ax, *map(sanitize_sequence, args), **kwargs) 1414 bound = new_sig.bind(ax, *args, **kwargs) 1415 auto_label = (bound.arguments.get(label_namer) 1416 or bound.kwargs.get(label_namer))
File ~/.local/lib/python3.8/site-packages/matplotlib/axes/_axes.py:3044, in Axes.pie(self, x, explode, labels, colors, autopct, pctdistance, shadow, labeldistance, startangle, radius, counterclock, wedgeprops, textprops, center, frame, rotatelabels, normalize) 3042 x = np.asarray(x, np.float32) 3043 if x.ndim > 1:
-> 3044 raise ValueError("x must be 1D") 3046 if np.any(x < 0): 3047 raise ValueError("Wedge sizes 'x' must be non negative values")
ValueError: x must be 1D
这里的1D表示一维。调用 pie 函数时,x 必须是一维值序列。在我看来,您正在将 DataFrame
分配给 x,它本质上是一个 table,因此它是二维的,即使它只有一行。如果您在调用 pie 之前添加一个 print(x.ndim)
,它应该打印 2 来表示这一点。
我相信只需将 x 替换为 x[0] 即可仅获取其中包含数据的行而不是 table 应该可以解决您的错误。
我正在尝试为每一列创建一个饼图并在两个用户之间进行比较,当我 运行 我的代码出现错误时:
ValueError: x must be 1D
我不确定它的真正含义,我试图查找它但找不到任何有用的信息,关于如何解决此问题的任何指导。 谢谢!!!!
# Aggregate dataframe
summarized_df = df.groupby(['user']).agg({ 'favorite_counts': 'sum', # Tweets overview
'retweet_counts': 'sum',
'is_positive': 'sum',
'is_negative': 'sum',
'tweets_length': 'sum', # Tweets writing style
'tweets_uppercase': 'sum',
'tweets_punctuations': 'sum',
'tweets_questionmark': 'sum',
'is_norp': 'sum', # Tweets detailed writing style
'is_time': 'sum',
'is_org': 'sum',
'is_gpe': 'sum',
'is_loc': 'sum',
'is_product': 'sum',
'is_workart': 'sum',
'is_fac': 'sum',
'is_noun': 'sum',
'is_pron': 'sum',
'is_adv': 'sum',
'is_propn': 'sum',
'is_verb': 'sum',
'is_intj': 'sum' }).reset_index()
# Seperate dataframe
Tim_summarized_df = summarized_df[summarized_df.user == 'Timothy Caulfield'].drop('user', axis = 1).copy()
Keran_summarized_df = summarized_df[summarized_df.user == 'Dr. Karen James'].drop('user', axis = 1).copy()
# Get columns
summarized_cols = Tim_summarized_df.columns
# Create features for each round in list format
round_1_cols = summarized_cols[:5]
round_2_cols = summarized_cols[5:10]
round_3_cols = summarized_cols[10:15]
round_4_cols = summarized_cols[15:20]
round_5_cols = summarized_cols[20:22]
# Combine all lists into one list
temp_list = [round_1_cols, round_2_cols, round_3_cols, round_4_cols, round_5_cols]
# Dictionary to rename the titles
rep_title_dict = { 'favorite_counts': 'Tweets Likes',
'retweet_counts': 'Re-Tweets',
'is_positive': 'Positivity',
'is_negative': 'Negativity',
'tweets_length': 'Length of Tweets',
'tweets_uppercase': 'Uppercase Characters Used',
'tweets_punctuations': 'Punctuations Used',
'tweets_questionmark': 'Questionmark Used',
'is_norp': 'Nationalities | Religious | Political Groups',
'is_time': 'Mentioned Time Related',
'is_org': 'Corporate | Governmental',
'is_gpe': 'Countries | Cities | States',
'is_loc': 'Location Mentioned',
'is_product': 'Objects | Vehicles | Foods',
'is_workart': 'Books | Songs',
'is_fac': 'Buildings | Airports | Highways',
'is_noun': 'Noun Used',
'is_pron': 'Pronoun Used',
'is_adv': 'Adverb Used',
'is_propn': 'Propn (like Apple, UK, US)',
'is_verb': 'Verb Used',
'is_intj': 'Bravo | Hello | Ouch' }
# Create function to plot summarized details
def summarized_donut_plot(data_1, data_2, indx_list):#, plot_title):
if indx_list == 4:
fig, ax = plt.subplots(ncols = 2, figsize= (13,6))
else:
fig, ax = plt.subplots(ncols = 5, figsize= (35,6))
for indx, target in enumerate(temp_list[indx_list]):
total = df[target].sum()
# Data preproccessing
x = round(float(data_1[target] / total * 100), 1)
y = round(float(data_2[target] / total * 100), 1)
#print(total, x, y)
user_list = ['Timothy', 'Karen']
results_list = [x, y]
x = pd.DataFrame(data = results_list, index = user_list)
ax[indx].pie(x, autopct='%1.1f%%', textprops = {'fontsize': 10, 'color': 'w', 'weight': 'bold'})
ax[indx].add_patch(plt.Circle((0,0), 0.35, fc = 'white'))
ax[indx].legend(user_list, loc = 2)
ax[indx].set_title(rep_title_dict[target], size = 10)
plt.show()
for indx, sublist in enumerate(temp_list):
summarized_donut_plot( data_1 = Tim_summarized_df,
data_2 = Keran_summarized_df,
indx_list = indx )
以下是我得到的错误。
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [170], in <cell line: 1>()
1 for indx, sublist in enumerate(temp_list):
----> 2 summarized_donut_plot( data_1 = Tim_summarized_df,
3 data_2 = Keran_summarized_df,
4 indx_list = indx )
Input In [169], in summarized_donut_plot(data_1, data_2, indx_list)
86 results_list = [x, y]
88 x = pd.DataFrame(data = results_list, index = user_list)
---> 90 ax[indx].pie(x, autopct='%1.1f%%', textprops = {'fontsize': 10, 'color': 'w', 'weight': 'bold'})
91 ax[indx].add_patch(plt.Circle((0,0), 0.35, fc = 'white'))
93 ax[indx].legend(user_list, loc = 2)
File ~/.local/lib/python3.8/site-packages/matplotlib/__init__.py:1412, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs) 1409 @functools.wraps(func) 1410 def inner(ax, *args, data=None,
**kwargs): 1411 if data is None:
-> 1412 return func(ax, *map(sanitize_sequence, args), **kwargs) 1414 bound = new_sig.bind(ax, *args, **kwargs) 1415 auto_label = (bound.arguments.get(label_namer) 1416 or bound.kwargs.get(label_namer))
File ~/.local/lib/python3.8/site-packages/matplotlib/axes/_axes.py:3044, in Axes.pie(self, x, explode, labels, colors, autopct, pctdistance, shadow, labeldistance, startangle, radius, counterclock, wedgeprops, textprops, center, frame, rotatelabels, normalize) 3042 x = np.asarray(x, np.float32) 3043 if x.ndim > 1:
-> 3044 raise ValueError("x must be 1D") 3046 if np.any(x < 0): 3047 raise ValueError("Wedge sizes 'x' must be non negative values")
ValueError: x must be 1D
这里的1D表示一维。调用 pie 函数时,x 必须是一维值序列。在我看来,您正在将 DataFrame
分配给 x,它本质上是一个 table,因此它是二维的,即使它只有一行。如果您在调用 pie 之前添加一个 print(x.ndim)
,它应该打印 2 来表示这一点。
我相信只需将 x 替换为 x[0] 即可仅获取其中包含数据的行而不是 table 应该可以解决您的错误。