如何用matplot绘制堆积柱状图

代码

%matplotlib inline
import matplotlib
import pandas as pd


df = pd.read_csv('/Users/yss/Downloads/Most-Recent-Cohorts-All-Data-Elements.csv', usecols=['INSTNM', 'REGION', 'ADM_RATE', 'SAT_AVG', 'COSTT4_A'] )

savedf = df

cleandf = df[df.ADM_RATE > 0]
df= cleandf

cleandf = df[df.SAT_AVG > 0]
df= cleandf


def sat(sat):
    try:
        t = sat/1000
    except ValueError:
        t = 0
    return t

def expense(tuition):
    try:
        t = tuition/50000
    except ValueError:
        t = 0
    return t

df.iloc[:, 3] = df.iloc[:, 3].apply(sat)
df.iloc[:, 4] = df.iloc[:, 4].apply(expense)


x= df[['REGION','SAT_AVG','ADM_RATE','COSTT4_A' ]]
y= x.set_index('REGION')
z=y.groupby('REGION').mean()

z.plot.bar(stacked=True)

代码解释

对上述代码各部分进行以下解释。
首先是用pandas读取数据我们指定的列,变成dataframe,并进行过滤,提取有效数据。

df = pd.read_csv('/Users/yss/Downloads/Most-Recent-Cohorts-All-Data-Elements.csv', usecols=['INSTNM', 'REGION', 'ADM_RATE', 'SAT_AVG', 'COSTT4_A'] )

savedf = df

cleandf = df[df.ADM_RATE > 0]
df= cleandf

cleandf = df[df.SAT_AVG > 0]
df= cleandf

然后我们定义了两个函数,对数据进行处理,这样可以使两列在同一数量级,画出的图形更加美观。

def sat(sat):
    try:
        t = sat/1000
    except ValueError:
        t = 0
    return t

def expense(tuition):
    try:
        t = tuition/50000
    except ValueError:
        t = 0
    return t

df.iloc[:, 3] = df.iloc[:, 3].apply(sat)
df.iloc[:, 4] = df.iloc[:, 4].apply(expense)

处理完成之后,将REGION字段作为我们的索引,这也是图像的x轴变量。我们以REGION做分组,然后求出每组的均值,最后使用z.plot.bar(stacked=True)绘制图形。

x= df[['REGION','SAT_AVG','ADM_RATE','COSTT4_A' ]]
y= x.set_index('REGION')
z=y.groupby('REGION').mean()

z.plot.bar(stacked=True)

遇到的问题

  1. 使用conda创建的虚拟环境,然后再使用pip安装完matplot之后,产生以下异常
Traceback (most recent call last):
  File "mp.py", line 2, in 
    import matplotlib.pyplot as plt
  File "/Users/yss/.anaconda/anaconda3/envs/py3.4/lib/python3.4/site-packages/matplotlib/pyplot.py", line 115, in 
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/Users/yss/.anaconda/anaconda3/envs/py3.4/lib/python3.4/site-packages/matplotlib/backends/__init__.py", line 62, in pylab_setup
    [backend_name], 0)
  File "/Users/yss/.anaconda/anaconda3/envs/py3.4/lib/python3.4/site-packages/matplotlib/backends/backend_macosx.py", line 17, in 
    from matplotlib.backends import _macosx
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.

按stack_over_flow上的方法可以解决。
具体是执行shell命令:

echo  backend: TkAgg > ~/.matplotlib/matplotlibrc

你可能感兴趣的:(如何用matplot绘制堆积柱状图)