I want to create a log plot
However, the result does not show the minor grid and something like this
What can I do with the script. Thank you!
import matplotlib.pyplot as plt
import numpy as np
# Data (x-values and y-values)
x = np.array([
3.17E-15, 6.34E-15, 1.27E-14, 2.54E-14, 5.07E-14, 1.01E-13, 2.03E-13, 4.06E-13, 8.11E-13,
1.62E-12, 3.21E-12, 6.38E-12, 1.27E-11, 2.54E-11, 5.07E-11, 1.01E-10, 2.03E-10, 4.06E-10,
8.11E-10, 1.62E-09, 3.21E-09, 6.38E-09, 1.27E-08, 2.54E-08, 5.07E-08, 1.01E-07, 2.03E-07,
4.06E-07, 8.11E-07, 1.62E-06, 3.21E-06, 6.38E-06, 1.27E-05, 2.54E-05, 5.07E-05,
0.00010144, 0.000202842, 0.000405646, 0.000684463, 0.001242097, 0.001340406,
0.002024869, 0.002737851
])
y = np.array([
42, 42, 42, 42, 41.9727, 41.9197, 41.819, 41.6368, 41.332, 40.879, 40.3057,
39.6555, 38.9779, 38.2946, 37.6106, 36.9265, 36.2423, 35.5579, 34.8729, 34.1871,
33.5108, 32.8299, 32.1462, 31.4607, 30.7743, 30.0873, 29.4001, 28.7127, 28.0254,
27.3381, 26.6609, 25.9797, 25.2959, 24.6105, 23.9243, 23.2377, 22.5509, 21.8641,
21.326, 20.735, 20.6422, 20.223, 19.9095
])
# Create semilog plot
plt.semilogx(x, y)
# Labels and title
plt.xlabel("X (Log Scale)")
plt.ylabel("Y")
plt.title("log x Plot")
# Add grid lines
plt.grid(which='both') # 'both' applies to both major and minor ticks
plt.minorticks_on() # Enable minor ticks
# Show the plot
plt.show()
I want the figure show something like this
You can use the answer from here, which in your case would be:
import matplotlib.pyplot as plt
# import ticker
import matplotlib.ticker
import numpy as np
# Data (x-values and y-values)
x = np.array([
3.17E-15, 6.34E-15, 1.27E-14, 2.54E-14, 5.07E-14, 1.01E-13, 2.03E-13, 4.06E-13, 8.11E-13,
1.62E-12, 3.21E-12, 6.38E-12, 1.27E-11, 2.54E-11, 5.07E-11, 1.01E-10, 2.03E-10, 4.06E-10,
8.11E-10, 1.62E-09, 3.21E-09, 6.38E-09, 1.27E-08, 2.54E-08, 5.07E-08, 1.01E-07, 2.03E-07,
4.06E-07, 8.11E-07, 1.62E-06, 3.21E-06, 6.38E-06, 1.27E-05, 2.54E-05, 5.07E-05,
0.00010144, 0.000202842, 0.000405646, 0.000684463, 0.001242097, 0.001340406,
0.002024869, 0.002737851
])
y = np.array([
42, 42, 42, 42, 41.9727, 41.9197, 41.819, 41.6368, 41.332, 40.879, 40.3057,
39.6555, 38.9779, 38.2946, 37.6106, 36.9265, 36.2423, 35.5579, 34.8729, 34.1871,
33.5108, 32.8299, 32.1462, 31.4607, 30.7743, 30.0873, 29.4001, 28.7127, 28.0254,
27.3381, 26.6609, 25.9797, 25.2959, 24.6105, 23.9243, 23.2377, 22.5509, 21.8641,
21.326, 20.735, 20.6422, 20.223, 19.9095
])
# Create semilog plot
plt.semilogx(x, y)
# Labels and title
plt.xlabel("X (Log Scale)")
plt.ylabel("Y")
plt.title("log x Plot")
ax = plt.gca()
# set major ticks (you need subs=(1.0,))
locmaj = matplotlib.ticker.LogLocator(base=10, subs=(1.0,), numticks=100)
ax.xaxis.set_major_locator(locmaj)
# set minor tick marks
locmin = matplotlib.ticker.LogLocator(base=10, subs=np.arange(2, 10) * .1, numticks=100)
ax.xaxis.set_minor_locator(locmin)
ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
# Add grid lines
ax.grid(which="both")
plt.show()
The main difference in this answer compared to, e.g., the answers in How to force and edit major and minor log plot ticks of pyplot subplot and How to display all minor tick marks on a semi-log plot, is the use of subs=(1.0,)
in the major tick locator, which seems to be required in this case to get the minor tick marks to show.