Forum Discussion
Anonymous
2 years agoNot 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 pa...
- Anonymous2 years ago
Hey guys!
I found out. Here is the final code:
import pandas as pdimport numpy as npimport matplotlib.pyplot as plt# Define the desired order for months and weekdaysmonths_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 columnperiod_column = dataset.columns[1] # Assumption: period column is the second columnvalue_column = dataset.columns[0] # Assumption: doubleValue column is the first column# Convert the period column to a categorical type with the correct orderif 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 boxplotdata = [dataset[dataset[period_column] == period][value_column].values for period in dataset[period_column].cat.categories]# Create the boxplotplt.boxplot(data, labels=dataset[period_column].cat.categories)# Add labelsplt.xlabel(period_column)# Show the plotplt.show()The first part deals with the order of months/days (otherwise it would be alphabetical)
Hope this can help somebody
Anonymous
2 years agoNot 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.
- Anonymous2 years agoNot applicable
Hi Anonymous,
thanks for your message. However, with everything I try, there is this error:
This error only occurs when I want to create a python visual, with a column diagram it works without problem.
Best regards,
Dominic