Issue
I want to plot a bar chart using Matplotlib. As the numbers are large, I am using a log scale for the Y-axis. But the common multiplier 10^7
of the Y-axis labels is missing from the plot. I want a single multiplier for the entire axis. Following is my snippet:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['hatch.linewidth'] = 2 # hatch linewidth
from matplotlib.ticker import ScalarFormatter
x1 = np.array([1.97001e+07,1.97001e+07,1.97001e+07,1.97001e+07])
x2 = np.array([1.89139e+07,1.89011e+07,1.88337e+07,1.87797e+07])
x3 = np.array([1.88874e+07,1.88337e+07,1.87647e+07,1.86836e+07])
fig, ax = plt.subplots(figsize=(12,6), dpi=150)
data1 = ax.bar(np.arange(4)-0.25, x1, color ='tab:red', width = 0.25, edgecolor='k',linewidth=2, hatch='/'*2)
data2 = ax.bar(np.arange(4)+0.00, x2, color ='tab:green', width = 0.25, edgecolor='k',linewidth=2, hatch='\\'*2)
data3 = ax.bar(np.arange(4)+0.25, x3, color ='gold', width = 0.25, edgecolor='k',linewidth=2, hatch='-')
ax.set_yscale('log')
ax.yaxis.set_minor_formatter(ScalarFormatter(useMathText=True))
ax.yaxis.set_major_formatter(ScalarFormatter(useMathText=True))
Please help to solve the problem. Thanks in advance.
Solution
After searching a lot, I could figure out how to display a multiplier 10^7
of the Y-axis labels. Hence, I am answering my own question and hope that it will help someone. It's not an ideal solution but a dirty way to display the exponent, and of course, it solved my problem. If there are any good alternatives, then please share. Here is the reference. My additional code is:
x_pos = 0.0
y_pos = 1.0
offset_text = r'x$\mathregular{10^{7}}$'
horizontalalignment='left'
verticalalignment='bottom'
ax.text(x_pos, y_pos, offset_text, transform=ax.transAxes,
horizontalalignment=horizontalalignment,
verticalalignment=verticalalignment)
Answered By - Ajinkya Bankar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.