Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

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.

 

 

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: 

 

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

 

 

 

  • Anonymous's avatar
    Anonymous
    2 years ago

    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)

     

     

    Hope this can help somebody

3 Replies

  • Anonymous's avatar
    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's avatar
      Anonymous
      Not 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

  • Anonymous's avatar
    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)

     

     

    Hope this can help somebody