执行以下代码时,我得到附图(我使用的是带有python 3.6和pycharm社区版的Windows 10)。
我找不到一种方法来使用scatter
并让我的'x'和'y'从0到最大值排序。
我很感激你的意见!
import pandas as pd
import matplotlib.pyplot as plt
X = ['1940','1300','1420','1680','1270','1850','1000','1100','1600','1000','2150','1900','2200','1100','860','1325','1350','1600','950','1250']
Y = ['1116000','860000','818400','1000000','640000','1010000','600000','700000','1100000','570000','860000','1085000','1250000','850000','640000','900000','730000','750000','650000','680000']
plt.scatter(X,Y)
plt.show()
答案 0 :(得分:2)
你的X和Y包含字符串,这会使用matplotlib搞砸了。如果将字符串转换为整数:轴就变得正确。
X = ['1940','1300','1420','1680','1270','1850','1000','1100','1600','1000','2150','1900','2200','1100','860','1325','1350','1600','950','1250']
Y = ['1116000','860000','818400','1000000','640000','1010000','600000','700000','1100000','570000','860000','1085000','1250000','850000','640000','900000','730000','750000','650000','680000']
Xs = [int(x) for x in X]
Ys = [int(y) for y in Y]
plt.scatter(Xs,Ys)
plt.show()
导致这个情节:
根据评论进行编辑:
您可以使用切片来指示要将数据转换为数字的数据。在下面的代码中,[int(x) for x in X[1:]]
部分指定使用列表中除索引0之外的每个项目。通过将结果保存在另一个变量中,列标题仍然在原始数据中。
X = ['header','1940','1300','1420','1680','1270','1850','1000','1100','1600','1000','2150','1900','2200','1100','860','1325','1350','1600','950','1250']
Y = ['other header','1116000','860000','818400','1000000','640000','1010000','600000','700000','1100000','570000','860000','1085000','1250000','850000','640000','900000','730000','750000','650000','680000']
Xs = [int(x) for x in X[1:]]
Ys = [int(y) for y in Y[1:]]
plt.scatter(Xs,Ys)
# bonus use the header as label
plt.xlabel(X[0])
plt.ylabel(Y[0])
plt.show()
此代码生成此图表:
或者你不能像这样使用中间变量:
plt.scatter([int(x) for x in X[1:]],[int(y) for y in Y[1:]])
# bonus use the header as label
plt.xlabel(X[0])
plt.ylabel(Y[0])
plt.show()
如果不需要制作中间变量,会产生与上面相同的图形。