Tom Patel
Tom Patel

Reputation: 432

Plotting a graph with matplotlib where X axis values are not evenly spread

I have used two columns from a DataFrame to plot a graph in matplotlib. Both columns have 568 values. The DataFrame column used for the X axis ( colA ) has values that start at 1.12 and finish at 13.11, however, a large majority of the X axis values are between 1.12 and 2.84 - approximately 415 values of the 568

I have created a simple line graph in matplotlib with the following code and output:

# Plot two columns in dataframe, which have 568 values each    
plt.plot(df.colA,df.colB,lw=2.5) 

# Array for x axis, which is col.A at set intervals 
x_axis_array = [ 1.12  1.2  1.24  1.28  1.33  1.38  1.44  1.5  1.57  1.65 1.73  1.79  1.84  1.9   1.96  2.03  2.08  2.11  2.16  2.22  2.28  2.34  2.4 2.46 2.55  2.68  2.84  3.07  3.3  3.61  3.98  4.52  5.08  5.84  6.55  8.59]

# Add array to the line chart      
plt.xticks((x_axis_array),fontsize=14,rotation=90)

plt.show()

The x_axis_array has 36 values and is basically comprised of values from df.colA at certain intervals.

I do not have enough reputation points to upload a image of the graph. Basically a majority the x-axis values are skewed to the left. They are clustered together and illegible. A few of the values are then spread out on the x axis taking up more space as you go further along the axis.

I would like to know if there is a way to, in effect, stretch out the graph towards the right, so that all the x axis values are spread out evenly? Thanks

Upvotes: 0

Views: 1168

Answers (1)

Molly
Molly

Reputation: 13610

A log scaled x axis might do what you want.

from matplotlib import pyplot as plt
import numpy as np

x_axis_array = [ 1.12,  1.2,  1.24,  1.28,  1.33,  1.38,
                 1.44,  1.5,  1.57,  1.65, 1.73,  1.79,
                 1.84,  1.9,   1.96,  2.03,  2.08,  2.11,
                 2.16,  2.22,  2.28,  2.34,  2.4, 2.46, 2.55,
                 2.68,  2.84,  3.07,  3.3,  3.61,  3.98,
                 4.52, 5.08, 5.84,  6.55,  8.59]

plt.plot(x_axis_array, np.ones((len(x_axis_array), 1)), '*')

ax = plt.gca()
ax.set_xscale('log')
plt.show()

log scaled x axis

Upvotes: 1

Related Questions