How do I draw a grid onto a plot in Python?

前端 未结 5 948
挽巷
挽巷 2020-12-04 06:06

I just finished writing code to make a plot using pylab in Python and now I would like to superimpose a grid of 10x10 onto the scatter plot. How do I do that?

My cur

相关标签:
5条回答
  • 2020-12-04 06:30

    The pylab examples page is a very useful source. The example relevant for your question:

    http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/scatter_demo2.py http://matplotlib.sourceforge.net/users/screenshots.html#scatter-demo

    0 讨论(0)
  • 2020-12-04 06:37

    Here is a small example how to add a matplotlib grid in Gtk3 with Python 2 (not working in 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()
    

    0 讨论(0)
  • 2020-12-04 06:45

    To show a grid line on every tick, add

    plt.grid(True)
    

    For example:

    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()
    


    In addition, you might want to customize the styling (e.g. solid line instead of dashed line), add:

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

    For example:

    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()
    

    0 讨论(0)
  • 2020-12-04 06:55

    You want to use 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.grid and ax.yaxis.grid can control grid lines properties.

    Enter image description here

    0 讨论(0)
  • 2020-12-04 06:57

    Using rcParams you can show grid very easily as follows

    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"
    

    If grid is not showing even after changing these parameters then use

    plt.grid(True)
    

    before calling

    plt.show()
    
    0 讨论(0)
提交回复
热议问题