使用用户输入访问嵌套列表以进行绘图
Accessing Nested List with user input for plotting
如何通过输入函数从下面的列表中获取值。
import matplotlib.pyplt as plt
List=[['China',25.5,26,27]['Germany',25,30.66,40]]
Years=['1999','2000']
y=input("Get Country Value:")
if y in List:
List2=List[y]
List2.plot(Years,y)
plt.show()`
需要像
这样的输出
Enter Country Name: China
plot with respect to year
首先我想指出本题的一些错误。然后我会解释两种实现方式。
您的 import
必须稍作修改,因为正确的语法是 import matplotlib.pyplot as plt
。
您的 List
变量必须正确实施。 List=[['China',25.5,26,27],['Germany',25,30.66,40]]
(中间使用Comma
)
现在,我假设您打算 plot
Years
使用 List
中的值,对应于给定的国家/地区名称 input
。但是,大小不匹配。我冒昧地在 Years
中添加了一个值。
第一个实现如下所示。
import matplotlib.pyplot as plt
List=[['China',25.5,26,27],['Germany',25,30.66,40]]
Years=['1999','2000',"2001"]
y=input("Get Country Value:")
for i in List:
if y in i:
List2 = i[1:]
plt.plot(Years,List2)
plt.show()
这可以通过为您的 List
变量使用不同的数据结构来稍微改进。即dictionary
。如果您还没有关于字典的想法,您可以阅读更多关于它们的信息 here。
其实现如下所示。
import matplotlib.pyplot as plt
List_as_dict = {"China":[25.5,26,27],
"Germany":[25,30.66,40]
}
Years=['1999','2000',"2001"]
y=input("Get Country Value:")
plt.plot(Years,List_as_dict[y])
plt.show()
这将消除不必要的循环,并且在处理非常大的数据时会更快一些。
我假设你列表中的值是对每一年的完全尊重。因此,为了可视化数据,我将使用 plt.bar 而不是 plt.plot 。所以我试试这个:
import matplotlib.pyplot as plt
import numpy as np
data = [['China',25.5,26,27],['Germany',25,30.66,40]]
years = ['1999','2000','2001']
labels = np.arange(len(years))
y = str(input('Input Country Name: '))
for i in data:
if i[0] == y:
newdata = i[1:len(i)]
fig, ax = plt.subplots()
ax.bar(labels, newdata,label=y)
ax.set_title('Total Respect For '+y+' Per Years')
ax.set_ylabel('Value')
ax.set_xticks(labels)
ax.set_xticklabels(years)
plt.show()
结果是:
Input Country Name: Germany
如何通过输入函数从下面的列表中获取值。
import matplotlib.pyplt as plt
List=[['China',25.5,26,27]['Germany',25,30.66,40]]
Years=['1999','2000']
y=input("Get Country Value:")
if y in List:
List2=List[y]
List2.plot(Years,y)
plt.show()`
需要像
这样的输出Enter Country Name: China
plot with respect to year
首先我想指出本题的一些错误。然后我会解释两种实现方式。
您的 import
必须稍作修改,因为正确的语法是 import matplotlib.pyplot as plt
。
您的 List
变量必须正确实施。 List=[['China',25.5,26,27],['Germany',25,30.66,40]]
(中间使用Comma
)
现在,我假设您打算 plot
Years
使用 List
中的值,对应于给定的国家/地区名称 input
。但是,大小不匹配。我冒昧地在 Years
中添加了一个值。
第一个实现如下所示。
import matplotlib.pyplot as plt
List=[['China',25.5,26,27],['Germany',25,30.66,40]]
Years=['1999','2000',"2001"]
y=input("Get Country Value:")
for i in List:
if y in i:
List2 = i[1:]
plt.plot(Years,List2)
plt.show()
这可以通过为您的 List
变量使用不同的数据结构来稍微改进。即dictionary
。如果您还没有关于字典的想法,您可以阅读更多关于它们的信息 here。
其实现如下所示。
import matplotlib.pyplot as plt
List_as_dict = {"China":[25.5,26,27],
"Germany":[25,30.66,40]
}
Years=['1999','2000',"2001"]
y=input("Get Country Value:")
plt.plot(Years,List_as_dict[y])
plt.show()
这将消除不必要的循环,并且在处理非常大的数据时会更快一些。
我假设你列表中的值是对每一年的完全尊重。因此,为了可视化数据,我将使用 plt.bar 而不是 plt.plot 。所以我试试这个:
import matplotlib.pyplot as plt
import numpy as np
data = [['China',25.5,26,27],['Germany',25,30.66,40]]
years = ['1999','2000','2001']
labels = np.arange(len(years))
y = str(input('Input Country Name: '))
for i in data:
if i[0] == y:
newdata = i[1:len(i)]
fig, ax = plt.subplots()
ax.bar(labels, newdata,label=y)
ax.set_title('Total Respect For '+y+' Per Years')
ax.set_ylabel('Value')
ax.set_xticks(labels)
ax.set_xticklabels(years)
plt.show()
结果是:
Input Country Name: Germany