英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论