【问题标题】:Graphing a line and scatter points using Matplotlib?使用 Matplotlib 绘制线和散点图?
【发布时间】:2023-03-13 12:05:01
【问题描述】:

我目前正在使用matplotlib 来尝试可视化我正在处理的一些数据。我试图在同一张图上绘制大约 6500 个点和线 y = x,但这样做时遇到了一些麻烦。我似乎只能得到要渲染的点,而不是线本身。我知道matplotlib 不会像这样绘制方程,而只是绘制一组点,所以我试图使用 x 和 y 坐标的相同点集来生成直线。

以下是我的代码

from matplotlib import pyplot
import numpy
from pymongo import *

class Store(object):
    """docstring for Store"""
    def __init__(self):
        super(Store, self).__init__()
        c = Connection()
        ucd = c.ucd
        self.tweets = ucd.tweets

    def fetch(self):
        x = []
        y = []
        for t in self.tweets.find():
            x.append(t['positive'])
            y.append(t['negative'])
        return [x,y]

if __name__ == '__main__':
    c = Store()
    array = c.fetch()
    t = numpy.arange(0., 0.03, 1)

    pyplot.plot(array[0], array[1], 'ro', t, t, 'b--')
    pyplot.show()

如有任何建议,将不胜感激,

帕特里克

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    如果我错了,请纠正我(我不是 matplotlib 专家),但 't' 只会得到值 [0.]。

    t = numpy.arange(0.,0.03,1)
    

    这意味着从 0 开始,到 0.03(不包括),步长为 1。导致数组只包含 0。

    在这种情况下,您只是在绘制一个点。做一条线需要两个人。

    【讨论】:

    • 非常感谢。我只知道这是我在做一些愚蠢的事情。效果很好。
    猜你喜欢
    • 1970-01-01
    • 2018-06-23
    • 2021-10-14
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    相关资源
    最近更新 更多