如何将卡片抽取模拟生成的结果数据导入散点图?
Hey there! I can help you get that surprise data into a scatter plot easily. Let's break this down step by step, using Matplotlib (the most common Python plotting library for this sort of thing).
First, let's fix your code to collect all the surprise values properly right now you're overwriting new_list every loop instead of adding to it. Then we'll add the plotting code.
Step 1: Update your code to collect all surprise values
Initialize an empty list at the start to store every surprise calculation from your nested loops. Then append each result to it instead of creating a new list each time:
from sequence import deckOrder import matplotlib.pyplot as plt # Add this import for plotting # Initialize an empty list to hold all surprise values all_surprises = [] for i in range(len(deckOrder)): for j in range(len(deckOrder)): #Draw first_draw = deckOrder[i] second_draw = deckOrder[j] #Before first draw initial_estimate = 0.5 initial_variance = 1/12 #After first draw r_count = first_draw.count('R') b_count = first_draw.count('B') alpha = 1 + r_count beta = 1 + b_count e_of_theta = alpha/(alpha+beta) surprise = ((e_of_theta - initial_estimate)**2)/(initial_variance) var_theta = (alpha * beta)/ ((alpha + beta) **2 *(alpha + beta + 1)) #After second draw r_count = second_draw.count('R') b_count = second_draw.count('B') new_alpha = alpha + r_count new_beta = beta + b_count new_e_of_theta = new_alpha/(new_alpha + new_beta) surprise = ((new_e_of_theta - e_of_theta)**2)/var_theta # Add the surprise value to our collection list all_surprises.append(surprise)
Step 2: Create a scatter plot
Now that we have all the surprise values stored, we can plot them. Here are two useful options depending on what you want to visualize:
Option 1: Basic scatter plot (all surprise values in order)
This shows the distribution of surprise across every possible draw combination, using the combination index as the x-axis:
# Create x-values as the index of each surprise entry x_values = range(len(all_surprises)) # Plot the scatter plot plt.scatter(x_values, all_surprises, alpha=0.6) # Alpha makes overlapping points easier to see plt.xlabel('Combination Index') plt.ylabel('Surprise Value') plt.title('Surprise Values from All Card Draw Combinations') plt.show()
Option 2: 2D Scatter Plot (i vs j with surprise as color)
If you want to see how each pair of (i,j) draws relates to surprise, this version maps surprise to point color for clearer context:
# Re-run the loop to collect i, j, and surprise together surprise_data = [] for i in range(len(deckOrder)): for j in range(len(deckOrder)): # ... [your existing calculation code here] ... surprise_data.append( (i, j, surprise) ) # Unpack the data into separate lists i_vals, j_vals, surprise_vals = zip(*surprise_data) # Create the 2D scatter plot scatter = plt.scatter(i_vals, j_vals, c=surprise_vals, cmap='viridis', alpha=0.7) plt.xlabel('First Draw Index (i)') plt.ylabel('Second Draw Index (j)') plt.title('Surprise by Card Draw Pair') plt.colorbar(scatter, label='Surprise Value') plt.show()
Quick Notes:
- If you don't have Matplotlib installed, run
pip install matplotlibin your terminal first. - The
alphaparameter adds transparency to points, which helps visualize density when multiple points overlap. - For the color scheme in Option 2, you can swap
viridisfor other built-in maps likeplasmaorcoolwarmif you prefer a different look.
That should get you a clear, actionable visualization of your surprise data!
内容的提问来源于stack exchange,提问作者user16462577

