绘制数据代码

时间:2017-04-24 20:41:25

标签: python pandas matplotlib plot graph

通过Python绘制数据

我们现有的图表如下:

import pandas as pd

import matplotlib.pyplot as plt
import numpy as np

#Sets the size of the chart
from pylab import rcParams
rcParams['figure.figsize'] = 7, 2

#Creates the dataframe
raw_data = {'plan_type': ['PPO', 'HMO', 'POS', 'CDHP', 'EPO', 'FFS'],
    'Your Plan':     [100, 0, 0, 100, 0, 0],
    'Benchmark':     [48, 16, 9, 22, 5, 0],
    'Region':     [18, 28, 84, 34, 11, 0],
    'Industry':      [49, 13, 7, 23, 6, 0],
    'Size':      [57, 16, 9, 26, 3, 0]

    }
df2 =pd.DataFrame(raw_data, columns = ['plan_type', 'Your Plan'])
df = pd.DataFrame(raw_data,
              columns = ['plan_type', 'Benchmark', 'Region', 'Industry',     
    'Size'])

#Plots the bars, adding desired colors
ax = df2.plot.bar(rot=0,color='#FFD966',width=1)
ax = df.plot.bar(rot=0, ax=ax, color=["#305496", '#8ea9db', '#b4c6e7',
'#D9E1F2'],
             width = 0.8 )


#Adds data labels to top of bars
for p in ax.patches[1:]:
h = p.get_height()
x = p.get_x()+p.get_width()/2.
if h != 0 :
    ax.annotate("%g" % p.get_height()+'%', xy=(x,h), xytext=(0,4),
rotation=90,
               textcoords="offset points", ha="center", va="bottom")

#Sets x-axis limits and margins
ax.set_xlim(-0.5, None)
ax.margins(y=0)

#Adds legend to the top of the page
ax.legend(ncol=len(df.columns), loc="lower left", bbox_to_anchor
(0,1.02,1,0.08),
      borderaxespad=0, mode="expand")

#Add labels to the x-axis
ax.set_xticklabels(df["plan_type"])

#shows the plot and prints it to
plt.show()[enter image description here][1]
plt.savefig("PlanOffered.png")

这变成了数字1,这很棒。但是,我们需要创建类似2号的东西。任何人都可以帮助我吗?

1

enter image description here

2

enter image description here 计算机希望我添加更多细节,所以在这里我浪费空间来满足机器。

0 个答案:

没有答案