Artikel ini akan membahas berbagai metode untuk menyimpan plot ke file gambar alih-alih menampilkannya menggunakan Matplotlib. Matplotlib adalah pustaka plot dengan Python yang digunakan untuk membuat gambar, bentuk, dan grafik. Show
Catatan. Sebelum mendalami metode untuk menyimpan plot ke file gambar, alih-alih menampilkannya menggunakan Matplotlib, Anda harus memastikan bahwa Anda telah menginstal modul Matplotlib di komputer Anda. Anda dapat menginstalnya dari Python Package Index menggunakan kode berikut. Setelah menginstal modul Membuat PlotAnda harus membuat plot terlebih dahulu menggunakan modul matplotlib. Program berikut menunjukkan cara membuat plot # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for the x-axis and y-axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Displaying the figure plt.show() Catatan. # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving the figure plt.savefig("image.jpg") # Interactive mode plt.show()0 digunakan untuk menentukan argumen untuk # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving the figure plt.savefig("image.jpg") # Interactive mode plt.show()1 dan # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving the figure plt.savefig("image.jpg") # Interactive mode plt.show()2 yang akan diplot Direkomendasikan Baca. Matplotlib Line Plot – Panduan Bergambar yang Bermanfaat Kami harus menyimpan gambar keluaran sebagai file gambar di disk. Jadi mari kita lihat berbagai cara untuk menyimpan plot ke file gambar alih-alih menampilkannya menggunakan Matplotlib Metode 1. Menggunakan metode savefig()Nama fungsi cukup jelas. Kami dapat menyimpan plot sebagai file gambar menggunakan metode # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving the figure plt.savefig("image.jpg") # Interactive mode plt.show()3. Metode # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving the figure plt.savefig("image.jpg") # Interactive mode plt.show()3 menyimpan plot dalam berbagai format seperti png, jpg, svg, pdf, dll Sintaksis. # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving the figure plt.savefig("image.jpg") # Interactive mode plt.show()_5
Catatan. Selalu pastikan bahwa fungsi # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving figure by changing the argument values plt.savefig("image", facecolor = 'y', bbox_inches = "tight", pad_inches = 0.2, transparent = True)_3 dipanggil (semua fitur harus ditentukan) sebelum fungsi # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving figure by changing the argument values plt.savefig("image", facecolor = 'y', bbox_inches = "tight", pad_inches = 0.2, transparent = True)4. Jika Anda memanggil fungsi # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving figure by changing the argument values plt.savefig("image", facecolor = 'y', bbox_inches = "tight", pad_inches = 0.2, transparent = True)_3 nanti, angka tersebut tidak akan disimpan sampai jendela plot ditutup Modus interaktifKode berikut akan menyimpan gambar sebagai “ # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving figure by changing the argument values plt.savefig("image", facecolor = 'y', bbox_inches = "tight", pad_inches = 0.2, transparent = True)6” di tujuan saat ini. Kita juga bisa menyimpan file plot hanya dengan mengklik icon simpan yang umumnya ditampilkan di bagian bawah jendela interaktif saat memanggil fungsi # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving figure by changing the argument values plt.savefig("image", facecolor = 'y', bbox_inches = "tight", pad_inches = 0.2, transparent = True)4 # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving the figure plt.savefig("image.jpg") # Interactive mode plt.show()_ Contoh berikut menunjukkan bagaimana kita dapat menyimpan gambar plot dengan mengubah nilai sesuai keinginan kita # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving figure by changing the argument values plt.savefig("image", facecolor = 'y', bbox_inches = "tight", pad_inches = 0.2, transparent = True) Mode Non-InteraktifDalam mode interaktif, gambar figur selalu ditampilkan meskipun fungsi # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving figure by changing the argument values plt.savefig("image", facecolor = 'y', bbox_inches = "tight", pad_inches = 0.2, transparent = True)4 tidak dipanggil setelah kita menyimpan plot di desktop kita. Namun, kita dapat mencegahnya dengan menutup paksa jendela gambar untuk menghentikan tampilan plot menggunakan metode # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving figure by changing the argument values plt.savefig("image", facecolor = 'y', bbox_inches = "tight", pad_inches = 0.2, transparent = True)9 Contoh # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving the figure plt.savefig("image.jpg") # Non-interactive mode plt.close() Untuk menghentikan tampilan gambar, kita juga dapat mematikan mode interaktif dengan menggunakan metode # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving the figure plt.savefig("image.jpg") # Non-interactive mode plt.close()0 Contoh # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving the figure plt.savefig("image.jpg") # Non-interactive mode plt.ioff() Metode 2. Menggunakan matplotlib. pyplot. imsave()Untuk menyimpan plot ke file gambar alih-alih menampilkannya menggunakan # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving the figure plt.savefig("image.jpg") # Non-interactive mode plt.close()1, kita dapat menggunakan metode # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving the figure plt.savefig("image.jpg") # Non-interactive mode plt.close()2. Metode ini menyimpan array sebagai file gambar Contoh # Importing the matplotliband NumPy module import matplotlib.pyplot as plt import numpy as np # Plotting using numpy's random img = np.random.randn(50, 100) # Saving the image file plt.imsave('image.png', img) KesimpulanSebagai penutup artikel, kami belajar cara menyimpan plot ke file gambar alih-alih menampilkannya menggunakan # Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x axis and y axis x = [10, 15, 20, 25, 30, 35] y = [x * 5 for x in x] # Plotting the values plt.plot(x, y) # Saving the figure plt.savefig("image.jpg") # Non-interactive mode plt.close()1. Saya harap artikel ini telah membantu Anda. Silakan menantikan dan berlangganan lebih banyak artikel semacam itu Matplotlib + Python adalah kombinasi canggih dalam visualisasi data dan ilmu data. Investasi waktu 4 jam ini akan membuat Anda menjadi pembuat kode yang lebih baik dan ilmuwan data yang lebih efektif Shubham Sayon Saya seorang Blogger Python profesional dan pembuat Konten. Saya telah menerbitkan banyak artikel dan membuat kursus selama periode waktu tertentu. Saat ini saya bekerja sebagai pekerja lepas penuh waktu dan saya memiliki pengalaman dalam domain seperti Python, AWS, DevOps, dan Networking Bagaimana cara menyimpan plot sebagai JPEG dengan Python?Untuk menyimpan gambar plot sebagai file JPG atau PNG, panggil fungsi savefig() pada matplotlib. objek pyplot. Berikan nama file bersama dengan ekstensi, sebagai argumen string, ke fungsi savefig().
Bagaimana cara menyimpan plot sebagai PNG dengan Python?Untuk menyimpan figur matplotlib sebagai gambar png, kita perlu menggunakan plt. savefig() dengan parameter yang relevan .
Bagaimana cara menyimpan model plot dengan python?Matplotlib plot dapat disimpan sebagai file gambar menggunakan plt. fungsi savefig() .
Bagaimana cara menyimpan plot sebagai PDF dengan Python?Untuk menyimpan file dalam format PDF, gunakan metode savefig() dengan nama gambar adalah myImagePDF. pdf, format = ”pdf” . Untuk menampilkan gambar, gunakan plt. tampilkan() metode. |