Matplotlib条形图技术咨询:数值显示异常且仅显示单一类型条形,求调整方案
Hey there! Let’s tackle those two Matplotlib bar chart issues you’re facing—let’s break down exactly what adjustments you need to make in each scenario.
Issue 1: Bar values aren’t showing in the correct position
To fix misaligned value labels, you’ll need to tweak how you calculate the text’s position relative to each bar, plus adjust alignment settings:
- Adjust the x-coordinate for text: Each bar has a starting x-position (
rect.get_x()) and width (rect.get_width()). Center the text horizontally by usingrect.get_x() + rect.get_width() / 2. If you’re working with grouped bars, make sure you account for the offset you added to each group’s bar positions when calculating this. - Tweak the y-coordinate: Use the bar’s height (
rect.get_height()) as the base, then add a small offset (like+0.5or-0.5for negative bars) to prevent the text from overlapping the bar itself. - Set proper alignment: Use
ha='center'(horizontal alignment) andva='bottom'(vertical alignment for positive bars; useva='top'for negative bars) to lock the text in place.
Here’s a quick snippet to illustrate:
# After creating your bars with ax.bar() rects = ax.bar(x_positions, bar_heights) for rect in rects: bar_height = rect.get_height() # Position text at the center-top of each bar ax.text( rect.get_x() + rect.get_width() / 2, bar_height + 0.3, # Small offset to avoid overlap str(bar_height), ha='center', va='bottom' )
Issue 2: Bar values aren’t showing at all, and only a single bar type displays
This has two parts—fixing the multi-bar display first, then adding the missing value labels:
- Enable multi-bar display: For grouped bars, you need to calculate offset positions for each group. For example, if you have 2 groups, set a bar width (like
0.35) and shift each group’s x-positions left/right by half the width. This prevents bars from overlapping and lets multiple types show up. - Add labels for all bar groups: Don’t forget to loop through every set of bars you create (e.g.,
rects1for Group 1,rects2for Group 2) and apply the same text-positioning logic from Issue 1 to each set. - Double-check your loops: Make sure you’re not only looping through one set of bars—each group needs its own label pass.
Example code for this scenario:
import matplotlib.pyplot as plt # Sample data categories = ['A', 'B', 'C'] group1_heights = [14, 22, 18] group2_heights = [16, 19, 25] x = range(len(categories)) bar_width = 0.35 fig, ax = plt.subplots() # Create two sets of bars with offset positions rects1 = ax.bar([i - bar_width/2 for i in x], group1_heights, bar_width, label='Group 1') rects2 = ax.bar([i + bar_width/2 for i in x], group2_heights, bar_width, label='Group 2') # Add labels to Group 1 bars for rect in rects1: height = rect.get_height() ax.text(rect.get_x() + rect.get_width()/2, height + 0.3, str(height), ha='center', va='bottom') # Add labels to Group 2 bars for rect in rects2: height = rect.get_height() ax.text(rect.get_x() + rect.get_width()/2, height + 0.3, str(height), ha='center', va='bottom') # Final tweaks to make the chart readable ax.set_xticks(x) ax.set_xticklabels(categories) ax.legend() plt.show()
内容的提问来源于stack exchange,提问作者Cullen DuYaw
相关产品推荐
相关产品推荐

