使用 Matplotlib 和 Pandas 时重命名 X 轴标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31536202/
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
Renaming X-Axis Labels when using Matplotlib and Pandas
提问by user2762934
I'm running my code on iPython Notebooks, on a Macbook Pro Yosemite 10.10.4
我在 iPython Notebooks 上运行我的代码,在 Macbook Pro Yosemite 10.10.4 上
I have a CSV file that I am trying to read using Python, and looking to come up with charts. The problem I am facing is renaming the X-Axis labels.
我有一个 CSV 文件,我正在尝试使用 Python 读取该文件,并希望提供图表。我面临的问题是重命名 X 轴标签。
Essentially, the chart is trying to plot a count of different types of Audit Violations, but has really long descriptions of the said violations. For example:
本质上,该图表试图绘制不同类型审计违规的计数,但对所述违规的描述非常长。例如:
- Not approved by regional committee.......another 300 words - 17
- No contract with vendor.......another 300 words - 14
- Vendor Registration not on record.......another 300 words - 9
- 未获区域委员会批准.......另外 300 字 - 17
- 没有与供应商签订合同.......另外 300 字 - 14
- 供应商注册未记录......另外 300 字 - 9
Instead of having these verbose reasons though, I would like to rename the X-Axis labels to just numbers or alphabets so that the graph reads somewhat like this:
虽然没有这些冗长的原因,但我想将 X 轴标签重命名为数字或字母,以便图表读起来有点像这样:
- A - 17
- B - 14
- C - 9
- A - 17
- 乙 - 14
- C - 9
This is the code I have used, and except for the label names, I am happy with the result.
这是我使用的代码,除了标签名称,我对结果很满意。
%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import pylab as pl
pd.set_option('display.mpl_style', 'default')
pd.set_option('display.width', 5000)
pd.set_option('display.max_columns', 60)
plt.rcParams['figure.figsize'] = (15, 5)
fixed_data = pd.read_csv('audit-rep.csv',sep=',',encoding='latin1',index_col='Index', parse_dates=['Report Date'],dayfirst=False)
viol_counts = data['Audit Flag'].value_counts()
viol_counts[:10]
viol_counts[:10].plot(kind='bar')
I have tried to rename the x-axis labels using the code below.
我尝试使用下面的代码重命名 x 轴标签。
viol_counts.set_ylabel('No. of Violations')
viol_counts.set_title('Audit Results')
viol_counts.set_xticks(ind+width)
viol_counts.set_xticklabels( ('A', 'B','C') )
This is the error I get when using the above code.
这是我在使用上述代码时遇到的错误。
AttributeError: 'Series' object has no attribute 'set_ylabel'
AttributeError: 'Series' 对象没有属性 'set_ylabel'
I have come across a few other posts related to this issue, but not seen one that specifically addresses the renaming of individual labels. This isn't utterly important though, and I'm just trying to learn using python, and the actual work has been done in excel.
我遇到了一些与此问题相关的其他帖子,但没有看到专门解决单个标签重命名的帖子。不过这并不是很重要,我只是想学习使用python,实际工作已经在excel中完成了。
采纳答案by tmdavison
if you assign the plot Axesobject to a variable name (here I've called it viol_plot), then you can perform action on that Axesobject (you are currently trying to set the labels and ticks on the Series, not the plot):
如果您将绘图Axes对象分配给一个变量名称(这里我称之为viol_plot),那么您可以对该Axes对象执行操作(您当前正在尝试在 上设置标签和刻度Series,而不是在绘图上):
viol_plot = viol_counts[:10].plot(kind='bar')
viol_plot.set_ylabel('No. of Violations')
viol_plot.set_title('Audit Results')
viol_plot.set_xticks(ind+width)
viol_plot.set_xticklabels( ('A', 'B','C') )
回答by Jihun
Methods such as set_ylabel, set_title are for plot object. One option would be using subplot:
set_ylabel、set_title 等方法用于绘图对象。一种选择是使用子图:
figure, ax = pl.subplots(1,1)
#note the ax option.
viol_counts[:10].plot(kind='bar', ax=ax)
ax.set_ylabel('No. of Violations')
ax.set_title('Audit Results')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('A', 'B','C') )
Or, you can just plot and use different functions. One downside is that I don't know how to set xticklabels in this case:
或者,您可以只绘制和使用不同的函数。一个缺点是我不知道在这种情况下如何设置 xticklabels:
viol_counts[:10].plot(kind='bar')
#note names of functions are slightly different
pl.ylabel('No. of Violations')
pl.title('Audit Results')
pl.xticks(ind+width)

