Issue
i have got a simple data set, which I need its x-axis values to be on a log base 2 scale. in addition, I used to type \Delta \angstrom or any mathematical expression as I would in LaTeX and the unit gets printed, It does not seem to work for me this time. any idea why?
my code:
import matplotlib.pyplot as plt
x = [0.35,0.38,0.47,0.53,0.6,0.7,0.84,1.53]
y = [0,0.36,0.87,2.84,7.58,11.25,14.33,19.64]
plt.plot(x, y)
plt.xlabel(r'Grid spacing [\angstrom]')
plt.ylabel(r'% error')
plt.title('x-y plot')
plt.show()
I would like to have my plot like the one below, notice the line is flipped, I tried inverting my x and y values but did not work:
Solution
Try this:
import matplotlib.pyplot as plt
x = [0.35,0.38,0.47,0.53,0.6,0.7,0.84,1.53]
y = [0,0.36,0.87,2.84,7.58,11.25,14.33,19.64]
plt.plot(x, y)
plt.xlabel(r'Grid spacing $\Delta\AA$')
plt.ylabel(r'% error')
plt.title('x-y plot')
plt.xscale('log', base=2)
plt.gca().invert_xaxis() # To invert the x axis
plt.show()
I supplied base
to the xscale
function to specify a base-2 log, and I changed your usage of \angstrom
to \AA
.
Answered By - K.Cl
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.