【问题标题】:How do I draw a grid onto a plot in Python?如何在 Python 中的绘图上绘制网格?
【发布时间】:2012-01-02 19:40:05
【问题描述】:

我刚刚完成了在 Python 中使用pylab 编写绘图的代码,现在我想在散点图上叠加一个 10x10 的网格。我该怎么做?

我当前的代码如下:

x = numpy.arange(0, 1, 0.05)
y = numpy.power(x, 2)

fig = plt.figure()
ax = fig.gca()
ax.set_xticks(numpy.arange(0, 1, 0.1))
ax.set_yticks(numpy.arange(0, 1., 0.1))
plt.scatter(x, y)
plt.show()

它的输出是:

我想要的是以下输出:

编辑:根据 Andrey Sobolev 的回答添加了一个示例

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    使用 rcParams 可以很容易地显示网格,如下所示

    plt.rcParams['axes.facecolor'] = 'white'
    plt.rcParams['axes.edgecolor'] = 'white'
    plt.rcParams['axes.grid'] = True
    plt.rcParams['grid.alpha'] = 1
    plt.rcParams['grid.color'] = "#cccccc"
    

    如果更改这些参数后网格仍未显示,则使用

    plt.grid(True)
    

    打电话之前

    plt.show()
    

    【讨论】:

      【解决方案2】:

      要在每个刻度上显示一条网格线,请添加

      plt.grid(True)
      

      例如:

      import matplotlib.pyplot as plt
      
      points = [
          (0, 10),
          (10, 20),
          (20, 40),
          (60, 100),
      ]
      
      x = list(map(lambda x: x[0], points))
      y = list(map(lambda x: x[1], points))
      
      plt.scatter(x, y)
      plt.grid(True)
      
      plt.show()
      


      此外,您可能想要自定义样式(例如实线而不是虚线),添加:

      plt.rc('grid', linestyle="-", color='black')
      

      例如:

      import matplotlib.pyplot as plt
      
      points = [
          (0, 10),
          (10, 20),
          (20, 40),
          (60, 100),
      ]
      
      x = list(map(lambda x: x[0], points))
      y = list(map(lambda x: x[1], points))
      
      plt.rc('grid', linestyle="-", color='black')
      plt.scatter(x, y)
      plt.grid(True)
      
      plt.show()
      

      【讨论】:

        【解决方案3】:

        你想使用pyplot.grid:

        x = numpy.arange(0, 1, 0.05)
        y = numpy.power(x, 2)
        
        fig = plt.figure()
        ax = fig.gca()
        ax.set_xticks(numpy.arange(0, 1, 0.1))
        ax.set_yticks(numpy.arange(0, 1., 0.1))
        plt.scatter(x, y)
        plt.grid()
        plt.show()
        

        ax.xaxis.gridax.yaxis.grid 可以控制网格线属性。

        【讨论】:

        • 知道为什么这会为我生成一个没有网格的情节吗?
        • 哦,我认为这是matplotlibrc 的问题,我将网格样式定义为实线,但它不再起作用了?
        • 其实应该可以的。至少,它对我有用——设置mpl.rcParams['grid.linestyle'] = "-" 确实会产生一个带有实线网格线的图。你的grid.linestyle 是什么?
        • 它是-。我不知道它为什么停止工作。总有一天我会调查的。
        • 另外,最好使用ax.scatter(x,y)ax.grid(True)
        【解决方案4】:

        这里是一个小例子,如何使用 Python 2 在 Gtk3 中添加 matplotlib 网格(不适用于 Python 3):

        #!/usr/bin/env python
        #-*- coding: utf-8 -*-
        
        import gi
        gi.require_version('Gtk', '3.0')
        from gi.repository import Gtk
        from matplotlib.figure import Figure
        from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas
        
        win = Gtk.Window()
        win.connect("delete-event", Gtk.main_quit)
        win.set_title("Embedding in GTK3")
        
        f = Figure(figsize=(1, 1), dpi=100)
        ax = f.add_subplot(111)
        ax.grid()
        
        canvas = FigureCanvas(f)
        canvas.set_size_request(400, 400)
        win.add(canvas)
        
        win.show_all()
        Gtk.main()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-07-06
          • 1970-01-01
          • 1970-01-01
          • 2019-02-05
          • 1970-01-01
          • 2019-03-25
          相关资源
          最近更新 更多