Search code examples
pythonmatplotlibpandasdata-analysis

Set x-axis intervals(ticks) for graph of Pandas DataFrame


I'm trying to set the ticks (time-steps) of the x-axis on my matplotlib graph of a Pandas DataFrame. My goal is to use the first column of the DataFrame to use as the ticks, but I haven't been successful so far.

My attempts so far have included:

Attempt 1:

#See 'xticks'
data_df[header_names[1]].plot(ax=ax, title="Roehrig Shock Data", style="-o", legend=True, xticks=data_df[header_names[0]])

Attempt 2:

ax.xaxis.set_ticks(data_df[header_names[0]])

header_names is just a list of the column header names and the dataframe is as follows:

    Compression Velocity   Compression Force  
1               0.000213            6.810879             
2               0.025055          140.693200            
3               0.050146          158.401500            
4               0.075816          171.050200             
5               0.101011          178.639500              
6               0.126681          186.228800              
7               0.150925          191.288300            
8               0.176597          198.877500        
9               0.202269          203.937000        
10              0.227466          208.996500         
11              0.252663          214.056000    

And here is the data in CSV format:

Compression Velocity,Compression Force
0.0002126891606,6.810879
0.025055073079999997,140.6932
0.050145696,158.4015
0.07581600279999999,171.0502
0.1010109232,178.6395
0.12668120459999999,186.2288
0.1509253776,191.2883
0.1765969798,198.8775
0.2022691662,203.937
0.2274659662,208.9965
0.2526627408,214.056

And here is an implementation of reading and plotting the graph:

data_df = pd.read_csv(file).astype(float)
fig = Figure()
ax = fig.add_subplot(111)
ax.set_xlabel("Velocity (m/sec)")
ax.set_ylabel("Force (N)")
data_df[header_names[1]].plot(ax=ax, title="Roehrig Shock Data", style="-o", legend=True)

The current graph looks like:

The x-axis is currently the number of rows in the dataframe (e.g. 12) rather than the actual values within the first column.

Is there a way to use the data from the first column in the dataframe to set as the ticks/intervals/time-steps of the x-axis?


Solution

  • This works for me:

    data_df.plot(x='Compression Velocity', y='Compression Force', xticks=d['Compression Velocity'])
    

    enter image description here