Issue
Background
Axis ticks can be converted to scientific format as suggested here.
Numbers can be converted into engineering format, one at a time as shown here
Question
How to format axis ticks in engineering notations i.e. order of magnitude is a multiple of 3.
Solution
Playing around with the decimal
module, I came around with the following solution:
from decimal import Decimal
import matplotlib.pyplot as plt
import numpy as np
data1 = np.linspace(-9, 9, 19)
data2 = 2.3 * 10**data1
yticks = 10**(np.linspace(-9, 9, 19))
yticklabels = [Decimal(y).quantize(Decimal('0.0000000001')).normalize().to_eng_string() for y in yticks]
plt.figure(1)
plt.subplot(121)
plt.grid(True)
plt.xlabel('k')
plt.ylabel('10^k')
plt.plot(data1, data2, 'k.')
plt.yscale('log')
plt.xticks(data1)
plt.yticks(yticks)
plt.subplot(122)
plt.grid(True)
plt.xlabel('k')
plt.ylabel('10^k')
plt.plot(data1, data2, 'k.')
plt.yscale('log')
plt.xticks(data1)
plt.yticks(yticks, yticklabels)
plt.show()
Please refer to the accepted answer on your second linked Q&A: Exponents between 0
and -6
are not converted to the desired format by definition/standard. Also, I needed to use the quantize
method from decimal
, too, because otherwise the outputted numbers would have had to many positions. (Remove the quantize
part, and you'll see, what I mean.)
Hope that helps!
Answered By - HansHirse
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.