pandas python - 使用数据框列更改 x 轴
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48633654/
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
python - change x axis using data frame column
提问by jvalenti
I have a data frame, df
I used to produce a plot of two series like so:
我有一个数据框,df
我用来生成两个系列的图,如下所示:
year = [2002, 2002, 2002, 2002]
month = ['Jan', 'Feb', 'Mar', 'Apr']
column1 = [3.3, 3.0, 3.1, 3.2, 2.9]
column2 = [7.0, 7.1, 7.3, 6.9, 7.3]
Dataset = list(zip(year, month, column1, column2))
df = DataFrame(data = Dataset, columns = ['year', 'month', 'column1', 'column2'])
df['column1'].plot(legend = True, label = 'column1')
df['column2'].plot(legend = True, label = 'column2', title = \
"Figure 1", style = '--', linewidth = 2.5)
Which produces the following:
产生以下结果:
I also have a column in my dataframe, df['year']
that has values that I would like to go along the x-axis. I tried the following
我的数据df['year']
框中还有一列,其中包含我想要沿 x 轴移动的值。我尝试了以下
plt.xticks(df['year'])
But the following happened:
但是发生了以下情况:
Is there a way to use the column df['year']
and have its values as the x axis tick marks without manually listing them? I would like the final version to look like the first plot but with the unique values of df['year']
along the x-axis.
有没有办法使用该列df['year']
并将其值作为 x 轴刻度线而不手动列出它们?我希望最终版本看起来像第一个图,但df['year']
沿 x 轴具有唯一值。
回答by ImportanceOfBeingErnest
To set the ticklabels to the values of some dataframe column, you would need to set the tickpositions to the index of the dataframe and the labels as the values from said column.
要将刻度标签设置为某个数据框列的值,您需要将刻度位置设置为数据框的索引,并将标签设置为来自所述列的值。
plt.xticks(df.index,df["year"].values)
Complete example:
完整示例:
from pandas import DataFrame
import matplotlib.pyplot as plt
year = [2002, 2002, 2002, 2002]
month = ['Jan', 'Feb', 'Mar', 'Apr']
column1 = [3.3, 3.0, 3.1, 3.2, 2.9]
column2 = [7.0, 7.1, 7.3, 6.9, 7.3]
Dataset = list(zip(year, month, column1, column2))
df = DataFrame(data = Dataset, columns = ['year', 'month', 'column1', 'column2'])
df['column1'].plot(legend = True, label = 'column1')
df['column2'].plot(legend = True, label = 'column2', title = \
"Figure 1", style = '--', linewidth = 2.5)
plt.xticks(df.index,df["year"].values)
plt.show()
This shows of course all labels as 2002, since all values from the year column are 2002. (Not sure if that makes sense though.)
这当然将所有标签显示为 2002,因为年份列中的所有值都是 2002。(但不确定这是否有意义。)
If you wanted to only label the first occurance of each year, you could use the unique years as follows
如果您只想标记每年的第一次出现,您可以使用独特的年份如下
unique_years, ind = np.unique(df["year"].values,return_index=True)
plt.xticks(df.index[ind], unique_years)
resulting in something like this:
导致这样的事情:
回答by romulomadu
You can set 'year'
columns as index first:
您可以'year'
先将列设置为索引:
df.set_index('year')
than you can use pandas to plot:
比你可以使用Pandas来绘制:
df[['column1','column2']].plot(title = 'Figure 1', legend = True, style = ['-','--'], linewidth = 2.5)
plt.show()
Pandas will print both series in same graph with index 'year'
as x axis, the columns names are automatically attributed as lines labels.
Pandas 将在同一个图表中打印两个系列,索引'year'
为 x 轴,列名自动归为行标签。