简体   繁体   English

使用 Matplotlib 和 Pandas 时重命名 X 轴标签

[英]Renaming X-Axis Labels when using Matplotlib and Pandas

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.我有一个 CSV 文件,我正在尝试使用 Python 读取该文件,并希望提供图表。 The problem I am facing is renaming the X-Axis labels.我面临的问题是重命名 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未经区域委员会批准.......另外 300 字 - 17
  • No contract with vendor.......another 300 words - 14没有与供应商签订合同.......另外 300 字 - 14
  • Vendor Registration not on record.......another 300 words - 9供应商注册未记录......另外 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 A - 17
  • B - 14乙 - 14
  • C - 9 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中完成了。

if you assign the plot Axes object to a variable name (here I've called it viol_plot ), then you can perform action on that Axes object (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') )

Methods such as set_ylabel, set_title are for plot object. set_ylabel、set_title 等方法用于绘图对象。 One option would be using subplot:一种选择是使用子图:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Pandas和Matplotlib在X轴上的日期 - Dates in X-axis using pandas and matplotlib 将Matplotlib(线图)与Pandas数据帧中的数据一起使用时,沿x轴缺少文本标签 - Missing textual labels along x-axis when using Matplotlib (line-plot) with data from a Pandas dataframe 更改 Pandas / Matplotlib 直方图中标签的 x 轴顺序? - Change x-axis order of labels in Pandas / Matplotlib histogram? 未在 pandas 数据框的 matplotlib 子图中显示 x 轴标签 - Not shown x-axis labels in matplotlib subplots from pandas dataframe Python - 使用 matplotlib 和 pandas 进行绘图时,x 轴使用 pandas 是准确的,但 matplotlib 不准确 - Python - When plotting using both matplotlib and pandas, the x-axis is accurate using pandas, but not matplotlib 使用来自 csv 的 matplotlib 和 pandas 的 Python 图表不显示所有 x 轴标签 - Python chart using matplotlib and pandas from csv does not show all x-axis labels 如何使用 matplotlib 在 x 轴条形图上插入标签? - How to insert labels on to x-axis bar graph using matplotlib? 如何使用 matplotlib 将月度标签添加到 x 轴? - How to add monthly labels to x-axis using matplotlib? 使用 matplotlib 修改直方图中的 x 轴标签 plot - modify the x-axis labels in histogram plot using matplotlib 使用 Matplotlib 绘制多索引数据框时 x 轴标签出错 - Error with x-axis labels when plotting multi-index dataframe using Matplotlib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM