How can I graph this data in dash plotly?

Question:

How can I print this in dash plotly?: article:

This is the code for matplotlib.plot (from the post) and I want to change it to dash plotly, how can I make this change? I’ve tried various things, but can’t manage to make it properly? Please help:

# Importing Packages
import matplotlib.pyplot as plt
import random

# Creating Roll Dice Function
def roll_dice():
    die_1 = random.randint(1, 6)
    die_2 = random.randint(1, 6)

    # Determining if the dice are the same number
    if die_1 == die_2:
        same_num = True
    else:
        same_num = False
    return same_num

# Inputs
num_simulations = 10000
max_num_rolls = 1000
bet = 1

# Tracking
win_probability = []
end_balance = []

# Creating Figure for Simulation Balances
fig = plt.figure()
plt.title("Monte Carlo Dice Game [" + str(num_simulations) + "   
          simulations]")
plt.xlabel("Roll Number")
plt.ylabel("Balance [$]")
plt.xlim([0, max_num_rolls])

# For loop to run for the number of simulations desired
for i in range(num_simulations):
    balance = [1000]
    num_rolls = [0]
    num_wins = 0    # Run until the player has rolled 1,000 times
    while num_rolls[-1] < max_num_rolls:
        same = roll_dice()        # Result if the dice are the same number
        if same:
            balance.append(balance[-1] + 4 * bet)
            num_wins += 1
        # Result if the dice are different numbers
        else:
            balance.append(balance[-1] - bet)

        num_rolls.append(num_rolls[-1] + 1)# Store tracking variables and add line to figure
    win_probability.append(num_wins/num_rolls[-1])
    end_balance.append(balance[-1])
    plt.plot(num_rolls, balance)

# Showing the plot after the simulations are finished
plt.show()

# Averaging win probability and end balance
overall_win_probability = sum(win_probability)/len(win_probability)
overall_end_balance = sum(end_balance)/len(end_balance)# Displaying the averages
print("Average win probability after " + str(num_simulations) + "   
       runs: " + str(overall_win_probability))
print("Average ending balance after " + str(num_simulations) + " 
       runs: $" + str(overall_end_balance))

Average win probability after 10000 simulations: 0.1667325999999987
Average ending balance after 10000 simulations: $833.663

I did a callback where I insert all this that’s before, I think the problem is with the balance, I don’t know how to save each balance in a dataframe to make the figure.

Asked By: vaginaweb

||

Answers:

A simple (and likely inefficient) way to do this is by using plotly’s add_trace() at the end of each iteration.

# Importing Packages
import plotly.graph_objects as go
import random


# Creating Roll Dice Function
def roll_dice():
    die_1 = random.randint(1, 6)
    die_2 = random.randint(1, 6)

    # Determining if the dice are the same number
    return die_1 == die_2


# Inputs
num_simulations = 10000
max_num_rolls = 1000
bet = 1

# Tracking
win_probability = []
end_balance = []

# Creating Figure for Simulation Balances
fig = go.Figure()

# For loop to run for the number of simulations desired
for i in range(num_simulations):
    balance = [1000]
    num_rolls = [0]
    num_wins = 0  # Run until the player has rolled 1,000 times
    while num_rolls[-1] < max_num_rolls:
        same = roll_dice()  # Result if the dice are the same number
        if same:
            balance.append(balance[-1] + 4 * bet)
            num_wins += 1
        # Result if the dice are different numbers
        else:
            balance.append(balance[-1] - bet)

        num_rolls.append(num_rolls[-1] + 1)  # Store tracking variables and add line to figure
    win_probability.append(num_wins / num_rolls[-1])
    end_balance.append(balance[-1])
    fig.add_trace(go.Scatter(x=num_rolls, y=balance))

# Showing the plot after the simulations are finished
fig.update_layout(title=f"Monte Carlo Dice Game [{num_simulations}simulations]",
                  showlegend=False,
                  xaxis_title="Roll number",
                  xaxis_range=[0, max_num_rolls],
                  yaxis_title="Balance [$]")

fig.show()

Notice how I change the number of simulations to 1000 since Plotly seemed to have trouble plotting this much data.

enter image description here

Answered By: debsim
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.