Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more
Hi, my code works perfectly in Spyder butwhen I paste here in PowerBI it gives below error and need support, thanks
Error Message:
Python script error.
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter
os.chdir(r'\Users\mishaq\OneDrive - Nokia\Machine Learning\DataSets')
default_bin_size = 2
default_smoothing_alpha = 0.1
default_use_average = False
input_data = 'For link curve RSRP vs Tput.csv'
df = pd.read_csv(input_data)
bin_size = default_bin_size
smoothing_alpha = default_smoothing_alpha
use_average = default_use_average
low_threshold = 25
plt.rcParams['font.family'] = "Arial"
x_label = 'NR_UE_RSRP_0'
y_label = 'Physical_Throughput_DL_5GNR'
selector = 'Logic'
########################################################################
df = df[[x_label, y_label, selector]]
df.dropna(how='any', axis=0, inplace=True)
# Bin the data
x = df[x_label]
y = df[y_label]
min_x = min(x)
max_x = max(x)
min_y = min(y)
max_y = max(y)
bins = np.arange(min_x, max_x + bin_size, bin_size)
x_binned = pd.cut(x, bins)
df['x_bin'] = x_binned
#output_file = input_data.split('.')[0]
fig = plt.figure(figsize=(8,5))
for logic in df[selector].unique():
df_ = df[df[selector] == logic]
# now obtain the aggregate
if use_average:
df_summarized = df_.groupby('x_bin').agg(lambda x: x.mean(skipna=True)).reset_index()
print('INFORMATION: Binning by the average function.')
else:
df_summarized = df_.groupby('x_bin').agg(lambda x: x.max(skipna=True)).reset_index()
print('INFORMATION: Binning by the max function.')
# if n is small, kill the sample with NaN since the statistic is insufficient.
df_summarized['N'] = df.groupby('x_bin').agg('count').reset_index()[x_label]
df_summarized.loc[df_summarized['N'] < low_threshold, y_label] = np.nan
df_summarized['x_fit'] = df_summarized['x_bin'].apply(lambda x: x.mid)
df_summarized.drop('x_bin', axis=1, inplace=True)
df_ = df_summarized[df_summarized[selector] == logic]
df_true = df[df[selector] == logic]
#df_summarized.dropna(inplace=True)
df_.fillna(method='ffill', inplace=True)
y_smooth = df_[y_label].ewm(alpha=smoothing_alpha).mean()
df_['y_fitted'] = gaussian_filter(y_smooth, sigma=1)
plt.scatter(df_true[x_label], df_true[y_label], alpha=0.2, label=f'Scatter {logic}')
plt.plot(df_['x_fit'], df_['y_fitted'], label=f'Best fit {logic}')
plt.grid(True, ls='dashed')
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.legend()
plt.tight_layout()
#plt.savefig(f'{output_file}.pdf', format='pdf')
plt.show()
plt.close(fig)
File "PythonScriptWrapper.PY", line 94
plt.scatter(df_true[x_label], df_true[y_label], alpha=0.2, label=f'Scatter {logic}')
^
SyntaxError: invalid syntax
Stack Trace:
Microsoft.PowerBI.ExploreServiceCommon.ScriptHandlerException: Python script error.
File "PythonScriptWrapper.PY", line 94
plt.scatter(df_true[x_label], df_true[y_label], alpha=0.2, label=f'Scatter {logic}')
^
SyntaxError: invalid syntax
---> Microsoft.PowerBI.Scripting.Python.Exceptions.PythonScriptRuntimeException: Python script error.
File "PythonScriptWrapper.PY", line 94
plt.scatter(df_true[x_label], df_true[y_label], alpha=0.2, label=f'Scatter {logic}')
^
SyntaxError: invalid syntax
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.