grok-3-preview-02-24· xAI
LMArena Human Preference 140K
instead of y axis add a total on top of bar
- User
instead of y axis add a total on top of bar
- Assistant
Below is a diff showing how to modify your code to remove the y-axis and instead add the total count on top of each stacked bar. This involves removing the y-axis labels and ticks, calculating the total count for each bin, and annotating the total on top of each bar.
--- a/your_script.py +++ b/your_script.py @@ -45,6 +45,7 @@ plt.figure(figsize=(12, 8)) bottom = np.zeros(len(bin_labels)) # to keep track of the lower bound of each stack +# Keep track of total counts per bin for annotation +total_counts = np.zeros(len(bin_labels)) + # Loop through each category and plot its counts on top of what was already plotted. for cat, color in zip(categories, colors): counts = stack_counts[cat] @@ -56,6 +57,7 @@ count = counts[i] if count > 2: # Only annotate if count > 2 to avoid clutter in small bars # Place the text at the center of the bar segment plt.text(bar.get_x() + bar.get_width()/2, bottom[i] + count/2, str(count), ha='center', va='center', fontsize=8, color='black') + # Update total counts for this bin + total_counts += counts # Update bottom to include the current category counts bottom += counts +# Add total count on top of each bar +for i, total in enumerate(total_counts): + if total > 0: # Only annotate if there is a total + plt.text(x_positions[i], total, str(int(total)), + ha='center', va='bottom', fontsize=10, color='black') + # Customize the plot appearance plt.xticks(x_positions, bin_labels, rotation=45, fontsize=12) -plt.yticks(fontsize=12) +plt.gca().yaxis.set_visible(False) # Hide the y-axis entirely plt.title('Distribution of Block Sizes by Supercategory', fontsize=16, fontweight='bold') plt.xlabel('Block Size', fontsize=14) -plt.ylabel('Count', fontsize=14) plt.legend(title='Supercategory', fontsize=12) plt.grid(axis='y', linestyle='--', alpha=0.7) +plt.grid(False) # Remove grid since y-axis is hidden plt.tight_layout()Explanation of Changes
-
Track Total Counts:
- Added
total_counts = np.zeros(len(bin_labels))to initialize an array to store the total counts for each bin. - Inside the loop,
total_counts += countsaccumulates the counts from each supercategory for each bin.
- Added
-
Annotate Totals on Top of Bars:
- Added a loop after the bar plotting to annotate the total count on top of each bar using
plt.text. The text is placed at the x-position of the bar (x_positions[i]) and just above the total height of the bar (total).
- Added a loop after the bar plotting to annotate the total count on top of each bar using
-
Remove Y-Axis and Grid:
- Replaced
plt.yticks(fontsize=12)withplt.gca().yaxis.set_visible(False)to completely hide the y-axis (ticks and labels). - Removed the y-axis label by deleting
plt.ylabel('Count', fontsize=14). - Disabled the grid with
plt.grid(False)since it’s not meaningful without a y-axis.
- Replaced
Result
- The y-axis will be hidden, making the plot cleaner and focusing attention on the bar heights and annotations.
- The total count for each bin will be displayed directly above each stacked bar, providing a clear summary of the data without needing a y-axis.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.