Сжатие графиков в matplotlib? - PullRequest
0 голосов
/ 20 мая 2019

Я новичок в python и matplotlib.Я просматривал приведенный ниже пример с этого сайта: https://glowingpython.blogspot.com/2012/01/how-to-plot-two-variable-functions-with.html, потому что я хочу найти наилучшую точность, учитывая скорость обучения и число эпох моей модели.Но когда я немного поиграл с ним, он уменьшился до небольшого размера графика.Как я могу заставить его поддерживать свой размер?Пример и мои изменения приведены ниже.

пример:

from numpy import exp, arange
from pylab import meshgrid, cm, imshow, contour, clabel, colorbar, axis, title, show


# the function that I'm going to plot
def z_func(x, y):
    return (1 - (x ** 2 + y ** 3)) * exp(-(x ** 2 + y ** 2) / 2)


x = arange(-3.0, 3.0, 0.1)
y = arange(-3.0, 3.0, 0.1)
X, Y = meshgrid(x, y)  # grid of point
Z = z_func(X, Y)  # evaluation of the function on the grid

im = imshow(Z, cmap=cm.RdBu)  # drawing the function
# adding the Contour lines with labels
cset = contour(Z, arange(-1, 1.5, 0.2), linewidths=2, cmap=cm.Set2)
clabel(cset, inline=True, fmt='%1.1f', fontsize=10)
colorbar(im)  # adding the colobar on the right
# latex fashion title
title('$z=(1-x^2+y^3) e^{-(x^2+y^2)/2}$')
show()

мои изменения:

from numpy import exp, arange
from pylab import meshgrid, cm, imshow, contour, clabel, colorbar, axis, title, show
import random

# the function that I'm going to plot
def z_func(x, y):
    return (x * y * random.random())


x = arange(0.001, 1.001, 0.001)
y = arange(1.0, 101.0, 1.0)
X, Y = meshgrid(x, y)  # grid of point
Z = z_func(X, Y)  # evaluation of the function on the grid

im = imshow(Z, cmap=cm.RdBu)  # drawing the function
# adding the Contour lines with labels
cset = contour(Z, arange(-1, 1.5, 0.2), linewidths=2, cmap=cm.Set2)
clabel(cset, inline=True, fmt='%1.1f', fontsize=10)
colorbar(im)  # adding the colobar on the right
# latex fashion title
title('title')
show()

1 Ответ

0 голосов
/ 20 мая 2019

Уменьшается из-за разницы в экстенте осей x и y в отличие от официального примера.Чтобы получить квадратную форму imshow, вам нужно отрегулировать соотношение сторон.Это можно сделать следующим образом здесь

import numpy as np
import matplotlib.pyplot as plt
import random

def z_func(x, y):
    return (x * y * random.random())

x = np.arange(0.001, 1.001, 0.001)
y = np.arange(1.0, 101.0, 1.0)
X, Y = np.meshgrid(x, y)  # grid of point
Z = z_func(X, Y)  # evaluation of the function on the grid

# Compute aspect ratio
dx = (x.max()-x.min()) / X.shape[0]
dy = (y.max()-y.min()) / X.shape[1]
dx_dy = dy/dx

im = plt.imshow(Z, cmap=cm.RdBu, aspect=dx_dy)  # drawing the function
cset = plt.contour(Z, np.arange(-1, 1.5, 0.2), linewidths=2, cmap=cm.Set2)
plt.clabel(cset, inline=True, fmt='%1.1f', fontsize=10)
plt.colorbar(im)  # adding the colobar on the right
plt.title('title')
plt.show()

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...