0

I'm trying to output a Pandas dataframe as a pyplot table to be inserted into a presentation, but I'm running into problems with the formatting of the values in the table itself.

This is one part of a much larger project, so I'll try to give as much code and context as I can:

values = []

for item in scenarios:
    value = dataframe.loc[dataframe['Scenario'] == item, 'Value'].sum()
    # Formatting for scientific notation with 2 decimal points (so, 3 sig figs)
    values.append("{:.2e}".format(value))

# Create a dataframe to hold this new, visual-ready data and add the generated values to it.
visual_data = pd.DataFrame(scens, columns=["Scenario"])
visual_data["Data"] = values
# Formatting the data as a float so Pandas can sort it properly.
visual_data["Data"] = visual_data["Data"].astype(float)
# Sorting. By default, Pandas sorts in descending order.
visual_data.sort_values(by=['Data'], inplace=True)

# Matplotlib code.
fig, ax = plt.subplots()
# fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')
table = ax.table(cellText=visual_data.to_numpy(), colLabels=visual_data.columns, loc='center', cellLoc='center')
fig.tight_layout()
plt.show()

The problem I run in to is how everything in the table itself displays. The data I'm working with runs from 1e-8 to 1e-4. When I just have my sorted dataframe everything is formatted in proper scientific notation from lowest to highest values.

As soon as I insert the data into the table (the '.to_numpy' statement when creating the table) the output looks something like the following:

 [['DUJ' 3.964e-08]
 ['DUE' 4.467e-08]
 ['DUD' 1.172e-07]
 ['DUC' 2.098e-07]
 ['DUG' 2.136e-07]
 ...
 ['DUN' 7.356e-05]
 ['MCC' 0.0001046]
 ['ALU' 0.0001652]]

With the final two entries rendering as standard floats instead of being consistent scientific notation like all the other entries in the table.

I know that Pandas' "set_printoption" has a "suppress=True" variable that should prevent this kind of behavior, but I can't figure out how to enable it (or disable it, as it were).

Any ideas?

  • I might have misunderstood your intent, but what is the purpose of using subplots? – PreciXon Aug 26 '21 at 02:26
  • Try this [link](https://stackoverflow.com/questions/31983341/using-scientific-notation-in-pandas) – woblob Aug 26 '21 at 14:38
  • @PreciXon I could have used ax.table() to create things directly, but some of the code snippets I came across online used a method like what I've used above. Whether a direct ax.table is made or not, I imagine Numpy's formatting will continue to be an issue. – P. Monroe Aug 30 '21 at 17:52
  • @woblob having tested things further, Pandas' "setoptions" function doesn't appear to be the offender here--it's the manner in which Numpy displays values within the generated table. As an example, here's some code output using the ".to_numpy()" call: ''' numpy.set_printoptions(precision=2) print(visual_data['Total'].to_numpy()) > [3.96e-08 4.47e-08 ... 7.36e-05 1.05e-04 1.65e-04] ''' So, everything's formatted correctly on a direct Numpy array. The issue comes later when I use the same .to_numpy() call lower down when specifying the table's cellText attribute. – P. Monroe Aug 30 '21 at 17:58

0 Answers0