Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python/Pandas-当x值为“时,将y值最小值设置为0”;“范围”;,不是整数_Python_Pandas - Fatal编程技术网

Python/Pandas-当x值为“时,将y值最小值设置为0”;“范围”;,不是整数

Python/Pandas-当x值为“时,将y值最小值设置为0”;“范围”;,不是整数,python,pandas,Python,Pandas,我正在创建一个柱状图,这是我的代码: dataset = {'Year': ["1950-1955", "1955-1960", "1960-1965", "1965-1970", "1970-1975", "1975-1980", "1980-1985", "1985-1990","1990-1995", "

我正在创建一个柱状图,这是我的代码:

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和df用于从CSV文件中获取此数据,我刚刚从中创建了一个数据集。我尝试做两件事,确保y值从0开始,最大值为10。这是图像:

如果我删除行“
plt.ylim('0.0','10.0')
”,我会得到以下结果:

我知道如果我将此添加到df4中:

df4 = df3[["Year", "Fertility Rate"]].astype(int)
它会自动将我的y值设置为零。然而,因为我的x值不是整数,它们是“范围”,所以这不起作用

问题:当使用日期“范围”的x值而不是整数时,如何将y值最小值设置为0并同时设置y值最大值?


我在Python API上找不到类似的问题。

您在不应该使用字符串的地方使用了字符串

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

我甚至无法复制和粘贴您的代码。修复一些东西你好,我删除了一行,移动了一些东西。对不起,如果你无法复制,我不知道还能做些什么。我直接从Spyder那里复制的。嗨,piR,谢谢你。我对python还是很陌生,所以数组、字符串、浮点的微妙之处让我难以理解。再次感谢。