Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 287
alacy Avatar asked Dec 15 '14 19:12

alacy


People also ask

How do you change the X-axis ticks in Python?

To plot the line chart, use the plot() method. To rotate the ticks at the x-axis, use the plt. xticks() method and pass the rotation argument to it.

How do you change axis increments in Python?

MatPlotLib with Python To change the range of X and Y axes, we can use xlim() and ylim() methods.

What are the MatPlotLib methods we can use to adjust our X-axis tick marks?

You can use the xticks() and yticks() functions and pass in an array denoting the actual ticks. On the X-axis, this array starts on 0 and ends at the length of the x array. On the Y-axis, it starts at 0 and ends at the max value of y . You can hard code the variables in as well.

How do I change the X-axis values in MatPlotLib?

In this article, we will be looking at the approach to set x-axis values in matplotlib in a python programming language. The xticks() function in pyplot module of the Matplotlib library is used to set x-axis values. List of xticks locations. Passing an empty list will remove all the xticks.


1 Answers

This works for me:

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

enter image description here

like image 143
BrenBarn Avatar answered Oct 13 '22 01:10

BrenBarn