So I'm creating a column graph and this is my code:
我创建了一个列图这是我的代码:
dataset = {'Year': ["1950-1955", "1955-1960", "1960-1965", "1965-1970", "1970-1975", "1975-1980", "1980-1985", "1985-1990","1990-1995", "1995-2000", "2000-2005", "2005-2010", "2010-2015", "2015-2020"],
'Fertility Rate': ['7.45 ','7.45', '7.45' ,'7.45' ,'7.45', '7.45' ,'7.45', '7.469' ,'7.654', '7.182' ,'6.371' ,'5.255' ,'4.412' ,'3.713']}
df3 = pd.DataFrame.from_dict(dataset)
df4 = df3[["Year", "Fertility Rate"]]
plt.bar(df4['Year'], df4['Fertility Rate'])
plt.title('Brazil')
plt.xticks(df4['Year'], rotation=90)
plt.xlabel('Year Range')
plt.ylabel('Fertility Rate')
plt.ylim('0.0','10.0')
plt.tight_layout()
plt.show()
Df2/Df1 and df were used to obtain this data from the CSV file, and I've just created a data set from it. I'm trying to do two things, ensure the y-values start from 0 and have a maximum of 10. This is the image:
Df2/Df1和df被用来从CSV文件中获取这个数据,我刚刚从它中创建了一个数据集。我要做两件事,确保y值从0开始,最大值为10。这是图片:
If I remove the line "plt.ylim('0.0','10.0')
", I get this:
如果我删除线"plt.ylim('0.0','10.0')",我得到了这个:
I know that if I add in this to df4:
我知道如果我把这个加到df4中:
df4 = df3[["Year", "Fertility Rate"]].astype(int)
It would automatically set my y-value to be zero. However, since my x-values are not integars, they're "range", this doesn't work.
它会自动将y值设为0。但是,由于x值不是整数,它们是“范围”,所以这行不通。
Question: How can I set the y-value minimum to be 0 and also set a y-maximum value, when using x-values that are a "range" of dates, rather than integars..
问题:当使用一个日期范围内的x值而不是整数时,如何将y值最小值设置为0,并设置y值最大值。
I couldn't find a similar question on the python API.
我在python API上找不到类似的问题。
Thank you.
谢谢你!
1 个解决方案
#1
2
You are using strings in places where you shouldn't
你在不该用的地方使用字符串
# Notice I wrapped your Fertility Rates in an array and made it float
# This is sloppy, fix it up on your side
dataset = {'Year': ["1950-1955", "1955-1960", "1960-1965", "1965-1970", "1970-1975", "1975-1980", "1980-1985", "1985-1990","1990-1995", "1995-2000", "2000-2005", "2005-2010", "2010-2015", "2015-2020"],
'Fertility Rate': np.array(['7.45 ','7.45', '7.45' ,'7.45' ,'7.45', '7.45' ,'7.45', '7.469' ,'7.654', '7.182' ,'6.371' ,'5.255' ,'4.412' ,'3.713'], float)}
df3 = pd.DataFrame.from_dict(dataset)
df4 = df3[["Year", "Fertility Rate"]]
df4.set_index('Year').plot.bar()
plt.xlabel('Year Range')
plt.ylabel('Fertility Rate')
# You had strings in the `ylim` definition. They need to be numbers.
plt.ylim(0, 10)
plt.tight_layout()
plt.show()
#1
2
You are using strings in places where you shouldn't
你在不该用的地方使用字符串
# Notice I wrapped your Fertility Rates in an array and made it float
# This is sloppy, fix it up on your side
dataset = {'Year': ["1950-1955", "1955-1960", "1960-1965", "1965-1970", "1970-1975", "1975-1980", "1980-1985", "1985-1990","1990-1995", "1995-2000", "2000-2005", "2005-2010", "2010-2015", "2015-2020"],
'Fertility Rate': np.array(['7.45 ','7.45', '7.45' ,'7.45' ,'7.45', '7.45' ,'7.45', '7.469' ,'7.654', '7.182' ,'6.371' ,'5.255' ,'4.412' ,'3.713'], float)}
df3 = pd.DataFrame.from_dict(dataset)
df4 = df3[["Year", "Fertility Rate"]]
df4.set_index('Year').plot.bar()
plt.xlabel('Year Range')
plt.ylabel('Fertility Rate')
# You had strings in the `ylim` definition. They need to be numbers.
plt.ylim(0, 10)
plt.tight_layout()
plt.show()