如何使用Python重叠图表?

huangapple go评论75阅读模式
英文:

How can i overlap graphs using Python?

问题

我正在尝试使用Python绘制一些图表。我使用了这个数据文件:https://datahub.io/core/global-temp/r/annual.csv

Annual = pd.read_csv("https://datahub.io/core/global-temp/r/annual.csv", sep=',', keep_default_na=False)

数据集包括相同时间线的两个来源。我想展示共同的图表以及它们各自的图表。还有三个图表。

alt.Chart(Annual, title= "1880-2016年的年度温度变化").mark_line().encode(
    alt.X('Year:O'),
    alt.Y('Mean'),
)

我已经能够从彼此过滤出不同的来源。

gcag = Annual.loc[0::2]
gistemp = Annual.loc[1::2]

如何将"Annual"(已经在工作中)和"gcag"、"gistemp"合并在一起?对于初学者,您有什么建议吗?也许不仅仅是线形图...

谢谢。

英文:

I am trying to do some graphs with Python. I used this data file: https://datahub.io/core/global-temp/r/annual.csv.

Annual = pd.read_csv("https://datahub.io/core/global-temp/r/annual.csv", sep=',', keep_default_na=False)

The Data Set includes 2 sources for the same timeline. I want to show the common graph and every one for their own. Also 3 Graphs.

alt.Chart(Annual, title= "Annual Temperature Change from 1880-2016").mark_line().encode(
    alt.X('Year:O'),
    alt.Y('Mean'),
)

I was able to filter sources from each other

gcag = Annual.loc[0::2]
gistemp = Annual.loc[1::2]

How can I bring "Annual"(which is already working) and "gcag", "gistemp" all together? Do you have any tips for an beginner? Maybe more than a line graph...

Ty.

答案1

得分: 1

我还没有使用过alt库的经验,但根据您的描述,以下是我做的可能对您有帮助的内容:

  • 数据集有两个不同的来源,您提到希望有一个命名为"common"的第三层。基于此,我假设您想要每年在GISTEMP和GCAG之间计算平均值。因此,以下是您可以尝试的代码片段:
avg_temp_by_year = Annual.groupby('Year').agg({'Mean':'mean'}).reset_index()
avg_temp_by_year['Source'] = 'ANNUAL'
  • 接下来,通过执行以下操作将这个新数据集追加到现有数据集中:
new_df = pd.concat([Annual, avg_temp_by_year], axis=0, ignore_index=True)
  • 绘制三个图表。对于此示例,我使用了seaborn库,它非常适合数据可视化:
import seaborn as sns
sns.lineplot(data=new_df, x='Year', y='Mean', hue='Source')

附注1:hue是一个参数,您可以传递它以在同一图上创建不同的图层。在此示例中,它接收了Source列,因为现在它包含了3个可能的值(ANNUAL、GISTEMP和GCAG)。

附注2:我鼓励您阅读文档(https://seaborn.pydata.org/generated/seaborn.lineplot.html)并尝试找到更好的方法来显示这些线条。

希望这个答案对您有所帮助!祝好运 如何使用Python重叠图表?

英文:

I haven't had any experience with the alt library yet, but based on what you described, here is what I did that might be helpful for you:

  • the dataset has 2 different sources and you mentioned that you wanted a third layer that you named as "common". Based on that, I assumed you want an average value between GISTEMP and GCAG, for each year. Thus, here is the snippet you could try:
avg_temp_by_year = Annual.groupby('Year').agg({'Mean':'mean'}).reset_index()
avg_temp_by_year['Source'] = 'ANNUAL'
  • next, append this new dataset to the pre-existing dataset, by doing:
new_df = pd.concat([Annual, avg_temp_by_year], axis=0, ignore_index=True)
  • plot the three graphs. For this example, I used the searbon library, which is a great one for data visualizations:
import seaborn as sns
sns.lineplot(data=new_df, x='Year', y='Mean', hue='Source')

PS1: hue is a parameter that you can pass to create different layers on the same graph. In this example, it receives the Source column, since it is comprised of the 3 possible values now (ANNUAL, GISTEMP and GCAG)

PS2: I encourage you to read the docs (https://seaborn.pydata.org/generated/seaborn.lineplot.html) and try to find a better way to display those lines.

I hope this answer helps you!
Good luck 如何使用Python重叠图表?

如何使用Python重叠图表?

huangapple
  • 本文由 发表于 2023年6月25日 22:32:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定