matplotlib 如何在Python中将网格绘制到图上?

mwkjh3gx  于 2023-04-21  发布在  Python
关注(0)|答案(5)|浏览(146)

我刚刚在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()

其输出为:

我想要的是以下输出:

oaxa6hgo

oaxa6hgo1#

你想使用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可以控制网格线属性。

t3irkdon

t3irkdon2#

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

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

jv2fixgn

jv2fixgn3#

使用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()
guz6ccqo

guz6ccqo4#

下面是一个小例子,如何使用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()

taor4pac

taor4pac5#

如果你想要更多的网格线比ticklabel,你可以这样做,使用小刻度。本质上,基本的想法是绘制小刻度的网格线,太。下面是一个例子,更多的网格线比ticklabel绘制。

import matplotlib.pyplot as plt

x = np.arange(0, 1, 0.05)
y = x**2

plt.scatter(x, y)
plt.xticks(np.arange(0, 1.01, 0.2))
plt.yticks(np.arange(0, 1.01, 0.2));

plt.xticks(np.arange(0, 1, 0.1), minor=True)   # set minor ticks on x-axis
plt.yticks(np.arange(0, 1, 0.1), minor=True)   # set minor ticks on y-axis
plt.tick_params(which='minor', length=0)       # remove minor tick lines

plt.grid()                                     # draw grid for major ticks
plt.grid(which='minor', alpha=0.3)             # draw grid for minor ticks on x-axis

如果您希望网格线比刻度标签少*************************************************************************************************************************************************************************************************************************************************

plt.scatter(x, y)
plt.xticks(np.arange(0, 1.01, 0.3))   # set major x-ticks
plt.yticks(np.arange(0, 1.01, 0.1))   # set major y-ticks

# set minor x-ticks
plt.xticks(np.arange(0, 1.01, 0.1), np.arange(0, 1.01, 0.1).round(1), minor=True)

# make the minor x-ticks have the same properties as the major ticks
# (this effectively overwrites the major ticks with the minor ones)
ax = plt.gca()
plt.tick_params(
    axis='x',
    which='minor', 
    pad=ax.xaxis.get_tick_padding(), 
    length=ax.xaxis.get_majorticklines()[0].get_markersize(), 
    width=ax.xaxis.get_majorticklines()[0].get_markeredgewidth())

# draw x-axis gridlines at major tick positions
plt.grid(axis='x')

如果网格线很少,则更简单的方法是在所需点上绘制垂直线(axvline)或水平线(axhline)。

plt.scatter(x, y)
plt.xticks(np.arange(0, 1.01, 0.1))
plt.yticks(np.arange(0, 1.01, 0.1))

# draw grid for every third point
for pt in np.arange(0, 1.01, 0.3):
    plt.axvline(pt, lw=0.5, color='black', alpha=0.5)

相关问题