【问题标题】:Matplotlib code not executing due to labels?由于标签,Matplotlib 代码没有执行?
【发布时间】: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 后:

enter image description here

【问题讨论】:

  • 如果您没有收到任何错误,则代码运行正常。那有什么问题呢?你在哪里/如何运行代码?
  • 对不起,我的意思是我的笔记本没有给我输出。
  • 您可能没有告诉笔记本如何输出任何内容。通常人们使用%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


【解决方案1】:

我得到一个分散。我没有更改您的代码中的任何内容。我所做的唯一更改是通过将代码plt.plot(x,y) 替换为plt.plot(x,y,'.') 来使其成为散点图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-30
    • 2015-12-03
    • 2013-08-10
    • 2019-06-22
    • 2010-11-23
    • 2012-09-24
    • 2012-09-17
    • 1970-01-01
    相关资源
    最近更新 更多