Home >Backend Development >Python Tutorial >How to Create and Annotate a Grouped Bar Chart Using Matplotlib in Python?
Plotting a grouped bar chart with Python's Matplotlib requires careful consideration of data manipulation, bar spacing, and labeling. Here's how to address your specific issue:
<code class="python">import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame(...).div(2233) ax = df.plot(kind='bar', color=colors, figsize=(20, 8), ylabel='Percentage', title="...") for p in ax.patches: ax.annotate(f'{p.get_height():0.2f}', (p.get_x() + p.get_width() / 2., p.get_height()), ha='center', va='center', xytext=(0, 10), textcoords='offset points')</code>
This code will generate a grouped bar chart with annotated bar heights.
The above is the detailed content of How to Create and Annotate a Grouped Bar Chart Using Matplotlib in Python?. For more information, please follow other related articles on the PHP Chinese website!