【问题标题】:Weird graph in PyPlotPyPlot 中的奇怪图形
【发布时间】:2019-05-18 23:09:40
【问题描述】:

我正在尝试声明一种从字典中绘制键和值的方法。字典表示一定时期内每年完成的科学工作量。

在我的主类中,我这样声明它,sci_prod 是字典:

def graphic(self, sci_prod):
    self.sci_prod = sci_prod
    x = list(sci_prod.keys())
    y = list(sci_prod.values())
    #plt.plot(x, y)
    #plt.xlabel("Year", fontsize=15)
    #plt.ylabel("Number of Completed Works", fontsize=15)
    #plt.show()

但我不断得到一个非常奇怪的图表,它以某种方式触及值,但中间有奇怪的形式。这是为什么呢?

【问题讨论】:

  • 这个问题没有很好的展开,看看here如何创建一个Minimal, Complete, and Verifiable example。
  • 问题中显示的代码有几种方式会产生“奇怪”的东西;同样有几种方法会产生一些不那么“奇怪”的东西。一切都取决于输入 - 你在这里保密。
  • 我认为这是因为您使用的是无序的线图 dict

标签: python class matplotlib graph


【解决方案1】:

应该这样做,在没有你的数据或看到奇怪的情节看起来像什么的情况下确认

def graphic(self, sci_prod):
    self.sci_prod = sci_prod
    x = sorted(list(sci_prod.keys()))
    y = [sci_prod[k] for k in x]
    plt.plot(x, y)
    plt.xlabel("Year", fontsize=15)
    plt.ylabel("Number of Completed Works", fontsize=15)
    plt.show()

【讨论】:

    猜你喜欢
    • 2021-10-19
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多