Python 为 Pandas DataFrame 的图形设置 x 轴间隔(刻度)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27491392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set x-axis intervals(ticks) for graph of Pandas DataFrame
提问by aus_lacy
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.
我正在尝试在 Pandas DataFrame 的 matplotlib 图上设置 x 轴的刻度(时间步长)。我的目标是使用 DataFrame 的第一列作为刻度,但到目前为止我还没有成功。
My attempts so far have included:
到目前为止,我的尝试包括:
Attempt 1:
尝试 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:
尝试 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:
header_names
只是列标题名称的列表,数据框如下:
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:
这是 CSV 格式的数据:
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.
x 轴当前是数据框中的行数(例如 12),而不是第一列中的实际值。
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?
有没有办法使用数据框中第一列的数据设置为 x 轴的刻度/间隔/时间步长?
采纳答案by BrenBarn
This works for me:
这对我有用:
data_df.plot(x='Compression Velocity', y='Compression Force', xticks=d['Compression Velocity'])