matplotlib双y轴绘图

# -*- coding: utf-8 -*-
"""
Created on Wed Jan 31 14:06:26 2018

@author: Administrator
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif']=['SimHei']#用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False#用来正常显示负号

path='F:/python/3python数据分析与挖掘实战/图书配套数据、代码/chapter3/demo/data/catering_sale_all.xls'
df=pd.read_excel(path,index_col='日期')
df.plot()

path='F:/python/3python数据分析与挖掘实战/图书配套数据、代码/chapter3/demo\data/catering_dish_profit.xls'
df1=pd.read_excel(path,index_col='菜品名')
plt.figure()
df1['盈利'].plot(kind='bar')
plt.ylabel('盈利')
plt.title('菜品盈利情况')

p = df1['盈利'].cumsum() / df1['盈利'].sum()
p.plot(color='r',secondary_y=True,style='-o',linewidth=2)
plt.ylabel('盈利(比例)')


x = np.arange(0., np.e, 0.01)
y1 = np.exp(-x)
y2 = np.log(x)

fig = plt.figure()

ax1 = fig.add_subplot(111)
ax1.plot(x, y1)
ax1.set_ylabel('Y values for exp(-x)')
ax1.set_title("Double Y axis")

ax2 = ax1.twinx()  # this is the important function
ax2.plot(x, y2, 'r')
ax2.set_xlim([0, np.e])
ax2.set_ylabel('Y values for ln(x)')
ax2.set_xlabel('Same X for both exp(-x) and ln(x)')

df1
Out[44]: 
      菜品ID    盈利
菜品名             
A1   17148  9173
A2   17154  5729
A3     109  4811
A4     117  3594
A5   17151  3195
A6      14  3026
A7    2868  2378
A8     397  1970
A9      88  1877
A10    426  1782

你可能感兴趣的:(python数据挖掘)