-1

I would like to change the value labels on my matplotlib bar chart to be in scientific notation. The values are in scientific notation in their original dataframe (I used the code line: pd.set_option('display.float_format', '{:.2e}'.format)), but not when labeled on my plot. This is how my current plot is looking like. I tried searching up the internet for a solution, but most stopped at the step of labeling bar charts.

My current code:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
pd.set_option('display.float_format', '{:.2e}'.format)
ax = dataframe.plot.bar(x='sample_name',y='mean (gc/mL)',rot=0,yerr='SD (gc/mL)', align='center', capsize=5)
ax.set_xlabel('Sample Name')
ax.set_ylabel('Concentration (gc/mL)')
ax.set_title('A6 Pretreatment Exp Concentration')

for bar in ax.patches:
  ax.text(bar.get_x() + bar.get_width() / 2,
          bar.get_height() / 2 + bar.get_y(),
          round(bar.get_height()), ha = 'center',
          color = 'w', size = 8)

plt.yscale("log")
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
plt.show()
  • 1. `ax = A6_summary_filtered.plot.bar(...)` 2. `ax.bar_label(ax.containers[0], fmt='%.2e', label_type='edge')` – Trenton McKinney Sep 01 '23 at 00:13
  • 1
    An alternative solution is to apply a formatter to your ax object (`plotdraft`). ```formatter = matplotlib.ticker.StrMethodFormatter('{x:.1E}') ax.yaxis.set_major_formatter(formatter) ax.yaxis.set_minor_formatter(formatter) ``` – Suriname0 Sep 01 '23 at 01:06

0 Answers0