Hướng dẫn plt.savefig python

Hướng dẫn plt.savefig python

Tôi đang cố gắng tiết kiệm các âm mưu mà tôi thực hiện bằng cách sử dụng matplotlib; tuy nhiên, các hình ảnh đang lưu trống.

Đây là mã của tôi:

plt.subplot(121)
plt.imshow(dataStack, cmap=mpl.cm.bone)

plt.subplot(122)
y = copy.deepcopy(tumorStack)
y = np.ma.masked_where(y == 0, y)

plt.imshow(dataStack, cmap=mpl.cm.bone)
plt.imshow(y, cmap=mpl.cm.jet_r, interpolation='nearest')

if T0 is not None:
    plt.subplot(123)
    plt.imshow(T0, cmap=mpl.cm.bone)

    #plt.subplot(124)
    #Autozoom

#else:
    #plt.subplot(124)
    #Autozoom

plt.show()
plt.draw()
plt.savefig('tessstttyyy.png', dpi=100)

Và tessstttyyy.png trống (cũng đã thử với .jpg)

  • python
  • image
  • matplotlib
  • figure

231 hữu ích 0 bình luận 226k xem chia sẻ

answer

361

Đầu tiên, điều gì xảy ra khi T0 is not None? Tôi sẽ kiểm tra điều đó, sau đó tôi sẽ điều chỉnh các giá trị mà tôi chuyển sang plt.subplot(); có thể thử các giá trị 131, 132 và 133 hoặc các giá trị phụ thuộc vào việc có T0tồn tại hay không .

Thứ hai, sau khi plt.show()được gọi, một hình mới được tạo. Để đối phó với điều này, bạn có thể

  1. Gọi plt.savefig('tessstttyyy.png', dpi=100)trước khi bạn gọiplt.show()

  2. Lưu con số trước bạn show()bằng cách gọi plt.gcf()"lấy con số hiện tại", sau đó bạn có thể gọi đối tượng savefig()này Figurebất kỳ lúc nào.

Ví dụ:

fig1 = plt.gcf()
plt.show()
plt.draw()
fig1.savefig('tessstttyyy.png', dpi=100)

Trong mã của bạn, 'tesssttyyy.png' để trống vì nó đang lưu hình mới, không có gì được vẽ trên đó.

361 hữu ích 5 bình luận chia sẻ

answer

150

plt.show() nên đến sau plt.savefig()

Giải thích: plt.show()rõ ràng toàn bộ sự việc, vì vậy bất cứ điều gì sau đó sẽ xảy ra trên một con số trống mới

150 hữu ích 3 bình luận chia sẻ

answer

18

thay đổi thứ tự của các chức năng đã khắc phục sự cố cho tôi:

  • đầu tiên Lưu cốt truyện
  • sau đó Hiển thị cốt truyện

như sau:

plt.savefig('heatmap.png')

plt.show()

18 hữu ích 0 bình luận chia sẻ

answer

3

Gọi savefig trước khi show () làm việc cho tôi.

fig ,ax = plt.subplots(figsize = (4,4))
sns.barplot(x='sex', y='tip', color='g', ax=ax,data=tips)
sns.barplot(x='sex', y='tip', color='b', ax=ax,data=tips)
ax.legend(['Male','Female'], facecolor='w')

plt.savefig('figure.png')
plt.show()

3 hữu ích 0 bình luận chia sẻ

answer

1

hãy để tôi đưa ra một ví dụ chi tiết hơn:

import numpy as np
import matplotlib.pyplot as plt


def draw_result(lst_iter, lst_loss, lst_acc, title):
    plt.plot(lst_iter, lst_loss, '-b', label='loss')
    plt.plot(lst_iter, lst_acc, '-r', label='accuracy')

    plt.xlabel("n iteration")
    plt.legend(loc='upper left')
    plt.title(title)
    plt.savefig(title+".png")  # should before plt.show method

    plt.show()


def test_draw():
    lst_iter = range(100)
    lst_loss = [0.01 * i + 0.01 * i ** 2 for i in xrange(100)]
    # lst_loss = np.random.randn(1, 100).reshape((100, ))
    lst_acc = [0.01 * i - 0.01 * i ** 2 for i in xrange(100)]
    # lst_acc = np.random.randn(1, 100).reshape((100, ))
    draw_result(lst_iter, lst_loss, lst_acc, "sgd_method")


if __name__ == '__main__':
    test_draw()

1 hữu ích 0 bình luận chia sẻ

answer

1

Hãy gọi plt.show()sau plt.savefig(fig)và vấn đề của bạn sẽ được giải quyết.

1 hữu ích 0 bình luận chia sẻ

Đăng nhập để trả lời câu hỏi

Có thể bạn quan tâm

Nội dung chính

  • Đăng nhập để trả lời câu hỏi
  • Có thể bạn quan tâm
  • Description
  • Save Current Figure to FIG-File
  • Save Multiple Figures to FIG-File
  • Save Figure Using 'compact' Option
  • Input Arguments
  • H — One or more figures single figure | array of figures
  • filename — File name 'Untitled.fig' (default) | character vector | string
  • 'compact' — File format for R2014b or later releases 'compact'
  • Version History

Tôi đang cố gắng tiết kiệm các âm mưu mà tôi thực hiện bằng cách sử dụng matplotlib; tuy nhiên, các hình ảnh đang lưu trống.

Đây là mã của tôi:

plt.subplot(121)
plt.imshow(dataStack, cmap=mpl.cm.bone)

plt.subplot(122)
y = copy.deepcopy(tumorStack)
y = np.ma.masked_where(y == 0, y)

plt.imshow(dataStack, cmap=mpl.cm.bone)
plt.imshow(y, cmap=mpl.cm.jet_r, interpolation='nearest')

if T0 is not None:
    plt.subplot(123)
    plt.imshow(T0, cmap=mpl.cm.bone)

    #plt.subplot(124)
    #Autozoom

#else:
    #plt.subplot(124)
    #Autozoom

plt.show()
plt.draw()
plt.savefig('tessstttyyy.png', dpi=100)

Và tessstttyyy.png trống (cũng đã thử với .jpg)

  • python
  • image
  • matplotlib
  • figure

231 hữu ích 0 bình luận 226k xem chia sẻ

answer

361

Đầu tiên, điều gì xảy ra khi T0 is not None? Tôi sẽ kiểm tra điều đó, sau đó tôi sẽ điều chỉnh các giá trị mà tôi chuyển sang plt.subplot(); có thể thử các giá trị 131, 132 và 133 hoặc các giá trị phụ thuộc vào việc có T0tồn tại hay không .

Thứ hai, sau khi plt.show()được gọi, một hình mới được tạo. Để đối phó với điều này, bạn có thể

  1. Gọi plt.savefig('tessstttyyy.png', dpi=100)trước khi bạn gọiplt.show()

  2. Lưu con số trước bạn show()bằng cách gọi plt.gcf()"lấy con số hiện tại", sau đó bạn có thể gọi đối tượng savefig()này Figurebất kỳ lúc nào.

Ví dụ:

fig1 = plt.gcf()
plt.show()
plt.draw()
fig1.savefig('tessstttyyy.png', dpi=100)

Trong mã của bạn, 'tesssttyyy.png' để trống vì nó đang lưu hình mới, không có gì được vẽ trên đó.

361 hữu ích 5 bình luận chia sẻ

answer

150

plt.show() nên đến sau plt.savefig()

Giải thích: plt.show()rõ ràng toàn bộ sự việc, vì vậy bất cứ điều gì sau đó sẽ xảy ra trên một con số trống mới

150 hữu ích 3 bình luận chia sẻ

answer

18

thay đổi thứ tự của các chức năng đã khắc phục sự cố cho tôi:

  • đầu tiên Lưu cốt truyện
  • sau đó Hiển thị cốt truyện

như sau:

plt.savefig('heatmap.png')

plt.show()

18 hữu ích 0 bình luận chia sẻ

answer

3

Gọi savefig trước khi show () làm việc cho tôi.

fig ,ax = plt.subplots(figsize = (4,4))
sns.barplot(x='sex', y='tip', color='g', ax=ax,data=tips)
sns.barplot(x='sex', y='tip', color='b', ax=ax,data=tips)
ax.legend(['Male','Female'], facecolor='w')

plt.savefig('figure.png')
plt.show()

3 hữu ích 0 bình luận chia sẻ

answer

1

hãy để tôi đưa ra một ví dụ chi tiết hơn:

import numpy as np
import matplotlib.pyplot as plt


def draw_result(lst_iter, lst_loss, lst_acc, title):
    plt.plot(lst_iter, lst_loss, '-b', label='loss')
    plt.plot(lst_iter, lst_acc, '-r', label='accuracy')

    plt.xlabel("n iteration")
    plt.legend(loc='upper left')
    plt.title(title)
    plt.savefig(title+".png")  # should before plt.show method

    plt.show()


def test_draw():
    lst_iter = range(100)
    lst_loss = [0.01 * i + 0.01 * i ** 2 for i in xrange(100)]
    # lst_loss = np.random.randn(1, 100).reshape((100, ))
    lst_acc = [0.01 * i - 0.01 * i ** 2 for i in xrange(100)]
    # lst_acc = np.random.randn(1, 100).reshape((100, ))
    draw_result(lst_iter, lst_loss, lst_acc, "sgd_method")


if __name__ == '__main__':
    test_draw()

1 hữu ích 0 bình luận chia sẻ

answer

1

Hãy gọi plt.show()sau plt.savefig(fig)và vấn đề của bạn sẽ được giải quyết.

1 hữu ích 0 bình luận chia sẻ

Đăng nhập để trả lời câu hỏi

Có thể bạn quan tâm

Main Content

Nội dung chính

  • Description
  • Save Current Figure to FIG-File
  • Save Multiple Figures to FIG-File
  • Save Figure Using 'compact' Option
  • Input Arguments
  • H — One or more figures single figure | array of figures
  • filename — File name 'Untitled.fig' (default) | character vector | string
  • 'compact' — File format for R2014b or later releases 'compact'
  • Version History

savefig

Save figure and contents to FIG-file

Syntax

Description

example

savefig(filename) saves the current figure to a FIG-file named filename.fig.

example

savefig(H,filename) saves the figures identified by the graphics array H to a FIG-file named filename.fig.

savefig(H,filename,'compact') saves the specified figures in a FIG-file that can be opened only in MATLAB® R2014b or later releases. The 'compact' option reduces the size of the .fig file and the time required to create the file.

Examples

collapse all

Save Current Figure to FIG-File

Create a surface plot of the peaks function. Save the figure to the file PeaksFile.fig.

figure
surf(peaks)
savefig('PeaksFile.fig')

To open the saved figure, use the command:

openfig('PeaksFile.fig');

MATLAB creates a new figure using the saved .fig file.

Save Multiple Figures to FIG-File

Create two plots and store the figure handles in array h. Save the figures to the file TwoFiguresFile.fig. Close the figures after saving them.

h(1) = figure;
z = peaks;
surf(z)

h(2) = figure;
plot(z)

savefig(h,'TwoFiguresFile.fig')
close(h)

To open the two figures, use the command:

figs = openfig('TwoFiguresFile.fig');

figs contains the handles of the two figures created.

Save Figure Using 'compact' Option

Save a figure using the compact option:

h = figure
surf(peaks)
savefig(h,'PeaksFile.fig','compact')

To open the figure, use the command:

openfig('PeaksFile.fig');

Input Arguments

collapse all

H — One or more figures single figure | array of figures

One or more figures, specified as a single figure or an array of figures.

filename — File name 'Untitled.fig' (default) | character vector | string

File name, specified as a character vector or string. If you do not specify a file name, then MATLAB saves the file as Untitled.fig, which is the default.

If the specified file name does not include a .fig file extension, then MATLAB appends the extension. savefig does not accept other file extensions.

Example: 'ExampleFile.fig'

Data Types: char | string

'compact' — File format for R2014b or later releases 'compact'

File format for R2014b or later releases of MATLAB, specified as 'compact'. This option results in smaller .fig files. However, do not use the 'compact' option if you want to open the .fig file in versions of MATLAB before R2014b.

Tips

  • You must use MATLAB to open files saved using savefig. To open the file, pass the file name to the function openfig or open. For example,

    openfig('ExampleFile.fig')
    opens the file, ExampleFile.fig, in MATLAB.
  • savefig saves the full MATLAB figure. To save only part of a figure, such as an axes, or to save handles in addition to the data, use the save function to create a MAT-file.

Version History

Introduced in R2013b