Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
Anonymous
Not applicable

Field Parameter pass to Python visual

Hey Guys!

 

I want to create a BoxPlot with a python chart. But I want to switch the x-axis between a week (Mon, Tue, ...) and month (jan, feb, ...). I think it should be possible to use a field parameter to create a slicer with the period to switch between month and day.

 

dominicghr_0-1718020851117.png

 

But how can I create my box plot? I want the period (week or month) on the X-axis and the doubleValue (from my value table) on the Y-axis. This is from my visualization pane: 

dominicghr_1-1718021878000.png

 

I don't know how to write the code correctly, or what the next step is to create the BoxPlot.

 

Can somebody help me please?

 

Best regards,

Dominic

 

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hey guys!

 

I found out. Here is the final code:

 

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Define the desired order for months and weekdays
months_order = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
days_order = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

# Determine the period column (Month or Day) and the doubleValue column
period_column = dataset.columns[1]  # Assumption: period column is the second column
value_column = dataset.columns[0]   # Assumption: doubleValue column is the first column

# Convert the period column to a categorical type with the correct order
if period_column == 'Month':
    dataset[period_column] = pd.Categorical(dataset[period_column], categories=months_order, ordered=True)
elif period_column == 'Day':
    dataset[period_column] = pd.Categorical(dataset[period_column], categories=days_order, ordered=True)

# Create the BoxPlot using matplotlib
# Create the list of data arrays for the boxplot
data = [dataset[dataset[period_column] == period][value_column].values for period in dataset[period_column].cat.categories]

# Create the boxplot
plt.boxplot(data, labels=dataset[period_column].cat.categories)

# Add labels
plt.xlabel(period_column)

# Show the plot
plt.show()

 

The first part deals with the order of months/days (otherwise it would be alphabetical)

 

dominicghr_0-1718114371220.png

 

Hope this can help somebody

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hey guys!

 

I found out. Here is the final code:

 

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Define the desired order for months and weekdays
months_order = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
days_order = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

# Determine the period column (Month or Day) and the doubleValue column
period_column = dataset.columns[1]  # Assumption: period column is the second column
value_column = dataset.columns[0]   # Assumption: doubleValue column is the first column

# Convert the period column to a categorical type with the correct order
if period_column == 'Month':
    dataset[period_column] = pd.Categorical(dataset[period_column], categories=months_order, ordered=True)
elif period_column == 'Day':
    dataset[period_column] = pd.Categorical(dataset[period_column], categories=days_order, ordered=True)

# Create the BoxPlot using matplotlib
# Create the list of data arrays for the boxplot
data = [dataset[dataset[period_column] == period][value_column].values for period in dataset[period_column].cat.categories]

# Create the boxplot
plt.boxplot(data, labels=dataset[period_column].cat.categories)

# Add labels
plt.xlabel(period_column)

# Show the plot
plt.show()

 

The first part deals with the order of months/days (otherwise it would be alphabetical)

 

dominicghr_0-1718114371220.png

 

Hope this can help somebody

Anonymous
Not applicable

Hi @Anonymous ,

First, you can click here for detailed guidance on creating and using field parameters.

Then you can refer to the following example and modify the Python code according to your actual situationtry:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

# Assuming 'dataset' is your DataFrame name and it includes 'period' and 'doubleValue' columns
# 'period' column should dynamically reflect the choice between week and month, based on the field parameter selection

# Convert the 'period' column to a categorical type to ensure correct ordering
dataset['period'] = pd.Categorical(dataset['period'], categories=[...], ordered=True)

# Create the BoxPlot
sns.boxplot(x='period', y='doubleValue', data=dataset)
plt.title('BoxPlot by Period')
plt.xlabel('Period')
plt.ylabel('Double Value')
plt.xticks(rotation=45)  # Rotate x-axis labels for better readability if needed
plt.show()

 

Best Regards,

Ada Wang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi @Anonymous,

 

thanks for your message. However, with everything I try, there is this error:

dominicghr_0-1718087461588.png

 

This error only occurs when I want to create a python visual, with a column diagram it works without problem.

 

 

Best regards,

Dominic

Helpful resources

Announcements
June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.