问题:创建数据集的图表时出现问题。

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

Problem with making a graph for a dataset

问题

我正在制作一个关于寿命数据集的图表,但进展不顺利,我想要添加死亡人数,并说明是哪个地区和年份。我尝试过多种方法,但要么图表太拥挤,文本重叠,要么出现错误。

我尝试了以下代码:

import pandas as pd
import matplotlib.pyplot as plt

life_expectancy_dataset = pd.read_csv("/kaggle/input/life-expectancy-who-updated/Life-Expectancy-Data-Updated.csv")

life_expectancy_dataset['Region'].value_counts()

plt.plot(life_expectancy_dataset["Year"])
plt.plot(life_expectancy_dataset["Region"])
plt.plot(life_expectancy_dataset["Life_expectancy"])
plt.title("Life Expectancy")
plt.ylabel("Life Expectancy")

plt.show()

图表不正确,底部右侧出现了文本重叠。

英文:

I am making a graph for a life expectancy data set, and it is not going well, I want to add the deaths and say from which region and year it is. I have tried multiple stuff but it either comes out cramped to the point where the text is overlapping, or it has errors.

I tried:

import pandas as pd
import matplotlib.pyplot as plt


life_expectancy_dataset=pd.read_csv("/kaggle/input/life-expectancy-who-updated/Life-Expectancy-Data-Updated.csv")

life_expectancy_dataset['Region'].value_counts()

plt.plot(life_expectancy_dataset["Year"])
plt.plot(life_expectancy_dataset["Region"])
plt.plot(life_expectancy_dataset["Life_expectancy"])
plt.title("Life Expectancy")
plt.ylabel("Life Expectancy")

plt.show()

and the graph wasn't right and there was overlapping text at the bottom right.

答案1

得分: 1

为了获得更好的图表,您应该尝试使用线图可视化不同地区随时间的预期寿命。

例如:

import pandas as pd
import matplotlib.pyplot as plt

life_expectancy_dataset = pd.read_csv("/kaggle/input/life-expectancy-who-updated/Life-Expectancy-Data-Updated.csv")

regions = life_expectancy_dataset['Region'].unique()

for region in regions:
    region_data = life_expectancy_dataset[life_expectancy_dataset['Region'] == region]
    region_data = region_data.sort_values(by='Year')
    plt.plot(region_data['Year'], region_data['Life_expectancy'], label=region)

plt.xlabel('Year')
plt.ylabel('Life Expectancy')
plt.title('Life Expectancy Over Time by Region')
plt.legend()
plt.show()
英文:

To have a better graph, you should try to visualize life expectancy over time across different regions with a line plot for each region.

For example:

import pandas as pd
import matplotlib.pyplot as plt

life_expectancy_dataset = pd.read_csv("/kaggle/input/life-expectancy-who-updated/Life-Expectancy-Data-Updated.csv")

regions = life_expectancy_dataset['Region'].unique()

for region in regions:
    region_data = life_expectancy_dataset[life_expectancy_dataset['Region'] == region]
    region_data = region_data.sort_values(by='Year')
    plt.plot(region_data['Year'], region_data['Life_expectancy'], label=region)

plt.xlabel('Year')
plt.ylabel('Life Expectancy')
plt.title('Life Expectancy Over Time by Region')
plt.legend()
plt.show()

huangapple
  • 本文由 发表于 2023年5月13日 19:27:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242482.html
匿名

发表评论

匿名网友

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

确定