programing tip

파이썬에서 플롯에 그리드를 그리는 방법은 무엇입니까?

itbloger 2020. 6. 23. 07:56
반응형

파이썬에서 플롯에 그리드를 그리는 방법은 무엇입니까? [닫은]


파이썬에서 pylab사용하여 플롯을 작성하는 코드 작성을 마쳤 으며 이제 10x10의 그리드를 산점도에 중첩하고 싶습니다. 어떻게합니까?


사용하고 싶습니다 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눈금 선 속성을 제어 할 수 있습니다.

여기에 이미지 설명을 입력하십시오


모든 눈금에 그리드 선을 표시하려면

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

여기에 이미지 설명을 입력하십시오


pylab 예제 페이지는 매우 유용한 소스입니다. 귀하의 질문과 관련된 예 :

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


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

다음은 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()

여기에 이미지 설명을 입력하십시오

참고 URL : https://stackoverflow.com/questions/8209568/how-do-i-draw-a-grid-onto-a-plot-in-python

반응형