我在为绘图指定自己的x和y值时遇到一些问题。 默认情况下,x和y是数据框列名的组合
例如,var_组合包含:
(('Cerebrum Volume', 'Cerebellum Volume'), ('Cerebrum Volume', 'Cerebellum Surface Area'), ('Cerebellum Volume', 'Cerebellum Surface Area'))
这在单独使用时非常有效,请参见下面的工作代码
col_names = [list(data.columns)[4], list(data.columns)[3], list(data.columns)[1]]
var_combinations = tuple(itertools.combinations(col_names, 2))
def create_plot(xy=var_combinations, logged=False):
if not logged:
fig1, axs1 = plt.subplots(1, (len(xy)), figsize=(16, 5))
for i, (x, y) in enumerate(xy):
axs1[i].scatter(data[x], data[y], c=taxon.map(colors), edgecolor='k')
axs1[i].set(
title=f'Primate {xy[i][0]} against\n{xy[i][1]}',
xlabel=f'{xy[i][0]}',
ylabel=f'{xy[i][1]}'
)
elif logged: .........
create_plot(var_combinations, logged=False)
但是,我希望在调用函数时能够输入自己的变量名,例如:
create_plot(('Cerebellum Surface Area', 'Cerebrum Volume'), ('etc', 'etc'))
当以这种方式调用时,当我点击for循环时,会得到一个ValueError: too many values to unpack (expected 2)
。如何解决此问题(并确保标题/轴标签和图形打印工作正常)
csv中的示例数据:
Species ,CerebellumSurfaceArea,CerebrumSurfaceArea,CerebellumVolume ,CerebrumVolume,Source,Taxon,,,,,,
Homo_sapiens,,,111.4,1088,MacLeod et al. (2003),Hominidae,,,,,,
Homo_sapiens,,,138.6,1387.1,,Hominidae,,,,,,
Homo_sapiens,,,132,1176.6,,Hominidae,,,,,,
Homo_sapiens,,,139.8,1264.5,,Hominidae,,,,,,
Homo_sapiens,1590,2038,,,Sereno et al. (2020),Hominidae,,,,,,
Homo_sapiens,1128,,,,Sultan and Braitenberg (1993),Hominidae,,,,,,
Pan_troglodytes,345.545,,52.1,383.22,Ashwell (2020),Hominidae,,,,,,
Pan_troglodytes,,,50.1,343.8,MacLeod et al. (2003),Hominidae,,,,,,
tuple
时发生。xy
应该是{(('CerebrumVolume', 'CerebellumVolume '), ('CerebrumVolume', 'CerebellumSurfaceArea'), ('CerebellumVolume ', 'CerebellumSurfaceArea'))
('Cerebellum Surface Area', 'Cerebrum Volume')
未正确解包李>xy=(('CerebrumVolume', 'CerebellumVolume '),)
用于向函数发送单个tuple
李>plt.subplots
将创建一个axes
而不是一个axes
数组squeeze=False
,这样即使只有一个axes
,也会始终有一个数组,然后使用axes.flatten()
展平数组李>相关问题 更多 >
编程相关推荐