简体   繁体   English

在Python中绘制趋势图

[英]Plotting a trend graph in Python

I have the following data in a DataFrame: 我在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   |
+----------------------+--------------+-------------------+

I want to plot a trend graph with the Program year on the x-axis and Value of Interest on the y-axis and different lines for each Physician Profile ID. 我想绘制一个趋势图,在x轴上显示“计划年度”,在y轴上显示“兴趣值”,并为每个医师个人资料ID分配不同的线条。 What is the best way to get this done? 完成这项工作的最佳方法是什么?

Two routes I'd consider going with this: 我会考虑的两条路线:

  • Basic, fast, easy: matplotlib , which would look something like this: 基本,快速,简单: matplotlib ,看起来像这样:
    • install it, like pip install matplotlib 安装它,就像pip install matplotlib
    • use it, like import matplotlib.pyplot as plt and this cheatsheet 使用它,例如import matplotlib.pyplot as plt此备忘单
  • Graphically compelling and you can drop your pandas dataframe right into it: Bokeh 图形引人入胜,您可以将熊猫数据框直接放入其中: Bokeh

I hope that helps you get started! 希望对您有所帮助!

I tried a few things and was able to implement it: 我尝试了几件事并能够实现它:

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()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM