用Python绘制趋势图

时间:2018-11-15 19:49:50

标签: python python-3.x plot

我在DataFrame中有以下数据:

+----------------------+--------------+-------------------+
| Physician Profile Id | Program Year | Value Of Interest |
+----------------------+--------------+-------------------+
| 1004777              |     2013     |    83434288.00    |
| 1004777              |     2014     |    89237990.00    |
| 1004777              |     2015     |    96321258.00    |
| 1004777              |     2016     |    186993309.00   |
| 1004777              |     2017     |    205274459.00   |
| 1315076              |     2013     |    127454475.84   |
| 1315076              |     2014     |    156388338.20   |
| 1315076              |     2015     |    199733425.11   |
| 1315076              |     2016     |    242766959.37   |
+----------------------+--------------+-------------------+

我想绘制一个趋势图,其中x轴为“计划年度”,y轴为“兴趣值”,并且每个医师个人资料ID的行都不同。完成这项工作的最佳方法是什么?

2 个答案:

答案 0 :(得分:1)

我会考虑的两条路线:

  • 基本,快速,简单:matplotlib,看起来像这样:
    • 安装它,就像pip install matplotlib
    • 使用它,例如import matplotlib.pyplot as pltthis cheatsheet
  • 图形引人入胜,您可以将熊猫数据框直接放入其中:Bokeh

希望对您有所帮助!

答案 1 :(得分:1)

我尝试了一些事情并能够实现它:

years = df["Program_Year"].unique()

PhysicianIds = sorted(df["Physician_Profile_ID"].unique())

pd.options.mode.chained_assignment = None

for ID in PhysicianIds:
    df_filter = df[df["Physician_Profile_ID"] == ID]
    for year in years:
        found = False
        for index, row in df_filter.iterrows():
            if row["Program_Year"] == year:
                found = True
                break
            else:
                found = False
        if not found:
            df_filter.loc[index+1] = [ID, year, 0]
    VoI = list(df_filter["Value_of_Interest"])
    sns.lineplot(x=years, y=VoI, label=ID, linestyle='-')

plt.ylabel("Value of Interest (in 100,000,000)")
plt.xlabel("Year")
plt.title("Top 10 Physicians")
plt.legend(title="Physician Profile ID")
plt.show()