python - SyntaxError:python 函数中的语法无效
问题描述
我没有在函数中看到问题,希望您能对以下问题有所了解。提前致谢。
Python函数:
def gen_histograms(dataframe, cols=1, file=None):
rows = math.ceil(len(dataframe.columns)/cols)
figwidth = 5 * cols
figheight = 4 * rows
fig, ax = plt.subplots(nrows = rows,
ncols = cols,
figsize = (figwidth, figheight))
color_choices = ['blue', 'grey', 'goldenrod', 'r', 'black', 'darkorange', 'g']
ax = ax.ravel() # Ravel turns a matrix into a vector... easier to iterate
for i, column in enumerate(dataframe.columns):
ax[i].hist(dataframe[column],
color=color_choices[i % len(color_choices)],
alpha = 1)
ax[i].set_title(f'{dataframe[column].name}', fontsize=18)
ax[i].set_ylabel('Observations', fontsize=14)
ax[i].set_xlabel('', fontsize=14)
fig.suptitle('\nHistograms for All Variables in Dataframe', size=24)
fig.tight_layout()
fig.subplots_adjust(bottom=0, top=0.88)
if file: plt.savefig(file, bbox_inches='tight')
plt.show();
return
错误:
File "<ipython-input-89-e68c3f9fb7c6>", line 24
ax[i].set_title(f'{dataframe[column].name}', fontsize=18)
^
SyntaxError: invalid syntax
解决方案
正如@Ryan Haining 所建议的,我已经修改了工作代码。
def gen_histograms(dataframe, cols=1, file=None):
rows = math.ceil(len(dataframe.columns)/cols)
figwidth = 5 * cols
figheight = 4 * rows
fig, ax = plt.subplots(nrows = rows,
ncols = cols,
figsize = (figwidth, figheight))
color_choices = ['blue', 'grey', 'goldenrod', 'r', 'black', 'darkorange', 'g']
ax = ax.ravel() # Ravel turns a matrix into a vector... easier to iterate
for i, column in enumerate(dataframe.columns):
ax[i].hist(dataframe[column],
color=color_choices[i % len(color_choices)],
alpha = 1)
ax[i].set_title(dataframe[column].name, fontsize=18)
ax[i].set_ylabel('Observations', fontsize=14)
ax[i].set_xlabel('', fontsize=14)
fig.suptitle('\nHistograms for All Variables in Dataframe', size=24)
fig.tight_layout()
fig.subplots_adjust(bottom=0, top=0.88)
if file: plt.savefig(file, bbox_inches='tight')
plt.show();
return
推荐阅读
- json - 以数字开头并以 K、M 和 G 结尾的 json 模式验证模式
- c++ - fork 和 execve 应用程序中的一些按顺序使用 condition_variable 和其他同时
- c# - 如何修复您的登录 asp.net mvc 以加载页面?
- android - RecyclerView 点击监听最佳实践且无内存泄漏
- python - 如何让matplotlib在没有font_manager的情况下显示外语
- sql - 在请求 oracle 中按日期选择(之间)
- javascript - 如果在 Vaadin 14 中调用 NPM 包的函数,则参考错误
- laravel - cookie 和 html 中的 CSRF 令牌不匹配
- flutter - 在颤动中转换部分可点击的文本
- java - 如何使用流从双精度数组中过滤掉非整数值?