Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to Export Matplotlib Plots to JPEG or PDF
Lohith
Lohith

Posted on

     

How to Export Matplotlib Plots to JPEG or PDF

To save a plot created using Matplotlib to a JPEG or PDF file, you can follow these steps:

  1. First, create your plot using Matplotlib. For example, let's say you have a simple line plot:

    importmatplotlib.pyplotaspltimportnumpyasnp# Generate some example datax=np.linspace(0,10,100)y=np.sin(x)# Create the plotplt.plot(x,y)plt.xlabel('X-axis')plt.ylabel('Y-axis')plt.title('Sine Wave')# Show the plot (optional)plt.show()

    Figure 1

2.After creating the plot, you can save it to a file using thesavefig function. Specify the filename and the desired format (JPEG or PDF). For example:

```python# Save the plot as a JPEG fileplt.savefig('my_plot.jpg', format='jpeg')# Save the plot as a PDF fileplt.savefig('my_plot.pdf', format='pdf')```
Enter fullscreen modeExit fullscreen mode

Replace'my_plot.jpg' and'my_plot.pdf' with your desired filenames.

3.The saved file will be in the same directory where your Python script is located.

Remember to adjust the plot and filenames according to your specific use case. If you have any other questions or need further assistance, feel free to ask! 😊

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Passionate and driven software engineer with a keen interest in exploring and mastering cutting-edge technologies.
  • Location
    Hyderabad, Telangana.
  • Pronouns
    He/Him
  • Work
    Software Engineer
  • Joined

More fromLohith

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp