Carlos Carvalho
Carlos Carvalho

Reputation: 107

Issue showing decimal places in X axis - Matplotlib

I've this issue with this graph,

Below the code:

axex=[0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0]
axey=[0.15686275, 0.28808446, 0.53242836, 1.        , 1.        ,
       1.        , 1.        ]

import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
#x = [0.00001,0.001,0.01,0.1,0.5,1,5]
plt.figure(figsize=(10,12))
plt.plot(axex,axey ,'o-', linestyle='--', label='Results')
plt.title("Show Test")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.legend(loc='best')
plt.show()

Whats's happening, is the values of "axex" and the line are not correctly displayed in the graph, It's not displaying in the axex scale (0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0)

If we run the graph without axex on the plot, we get the correct line graph, but the scale of x axis is not correctly displayed.

How can we fix this ?

Upvotes: 0

Views: 3156

Answers (1)

Wololo
Wololo

Reputation: 1295

If x and y are two lists of numerical values, then plot(x,y) will plot all the coordinates (x[0], y[0]), (x[1], y[1]), ..., and so on, and by default, it will only use linear axes. plot(x,y) will not change the scale of the axes depending on the numerical values of the data stored in x and y.

One way to get a logarithmic scale, is to use pyplot.xscale('log') and/or equivalently for the y-scale, yscale('log').

import matplotlib.pyplot as plt

axex=[0.001, 0.01, 0.1, 1.0, 10.0, 100.0, 1000.0]
axey=[0.15686275, 0.28808446, 0.53242836, 1., 1., 1., 1.]

plt.figure()
plt.plot(axex, axey, linestyle='--', label='Results')
plt.xscale('log') # Set log scale on X-axis
plt.title("Show Test")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.show()

enter image description here

Upvotes: 1

Related Questions