【发布时间】:2019-04-22 04:43:27
【问题描述】:
我正在使用 salem_executions_data.csv,它是这样的:
year,month,accusations,executions
1962,1,0,0
1962,2,3,0
1962,3,4,0
1962,4,22,0
1962,5,39,0
1962,6,3,1
1962,7,12,5
1962,8,23,5
1962,9,33,9
1962,10,1,0
1962,11,3,0
1962,12,0,0
1963,1,0,0
1963,2,0,0
1963,3,0,0
使用 Python 2.7,我想要的是:
- 加载 csv 文件
- 使用 3d scatter 进行指控与处决
到目前为止,我所拥有的是:
import matplotlib.pyplot as plt
import csv
x = []
y = []
with open('data/salem_executions_data.csv','r') as csvfile:
plots = csv.reader(csvfile, delimiter=',')
from itertools import islice
for row in islice(plots,1,None):
for row in plots:
temp=row[int(str(2))]
x.append(temp)
temp=row[int(str(3))]
y.append(temp)
plt.plot(x,y)
plt.xlabel('Accusations')
plt.ylabel('Executions')
plt.title('Accusations vs Executions')
plt.legend()
plt.show()
执行此单元格后,我没有得到任何输出代码...有什么帮助吗?
添加 %matplotlib inline 后,这是我得到的输出,你觉得可以吗? enter image description here
更新 v3:
将 plt.plot 改为 plt.scatter 后:
【问题讨论】:
-
如果您没有收到任何错误,则代码运行正常。那有什么问题呢?你在哪里/如何运行代码?
-
对不起,我的意思是我的笔记本没有给我输出。
-
您可能没有告诉笔记本如何输出任何内容。通常人们使用
%matplotlib inline或%matplotlib notebook。您也可以使用%matplotlib tk或%matplotlib qt。 -
它说 /usr/local/lib/python2.7/dist-packages/matplotlib/axes/_axes.py:545:用户警告:未找到标记的对象。在个别地块上使用 label='...' kwarg。 warnings.warn("没有找到标记的对象。"
-
如果您想要散点图而不是线图,您可能需要调用
plt.scatter(x,y)而不是plt.plot(x,y)。您可能需要对 x,y 点进行排序。
标签: python python-2.7 matplotlib