It is required to save the created charts in different ways depending on our needs. If we need to reproduce our chart in different notebooks or Python sessions, or reuse them in future projects, it is a good practice to save the Python code. If we need to make reports or presentations, it can be very useful to save our chart as an image. It is also possible to save our chart as a HTML page, and this
could be very useful when we need to share your work on Web. Let's how to save our chart in different ways:
1. Saving the Code
If IPython is used for coding then this trick is useful in which the code concerning the
representation of a single chart is saved in a .py file using the save% command followed by the name of the file you want to save followed by the number of input prompts containing the row of code that you want to save. If all the code is written in only one prompt, as in our case, we have to add only
its number; otherwise if we want to save the code written in many prompts, for example from 10 to 20, we have to indicate this range with the two numbers separated by a -, that is, 10-20.
Suppose in our case, we would save the Python code underlying the representation of our first chart contained into the input prompt with the number 171.
In [171]: import matplotlib.pyplot as plt
...
We need to insert the following command to save the code into a new .py file.
%save my_first_chart 171
After we launch the command, we will find the my_first_chart.py file in our working directory. Later, when we open a new IPython session, we will have our chart and start to change the code at the point where we had saved it by entering the following command:
ipython qtconsole --matplotlib inline -m my_first_chart.py
Or we can reload the entire code in a single prompt in the QtConsole using the command %load.
%load my_first_chart.py
Or We can run it during a session with the command %run.
%run my_first_chart.py
2. Converting Your Session to an HTML File
Using the IPython QtConsole, we can convert all the code and graphics present in our current session to an HTML page. Simply choose File-->Save to HTML/XHTML from the menu as shown below:
As shown in the figure above we will be asked to save our session in two different formats: HTML and XHMTL. The difference between the two formats is based on the image conversion type.
If you select HTML as the output file format, the images contained in our session will be converted to PNG format. If we select XHTML as the output file format instead, the images will be converted to SVG format.
Let's our session as an HTML file and name it my_session.html, as shown in Figure below:
Next we'll be asked if you want to save your images in an external directory or inline
By choosing the external option, the images will be collected into a directory called my_session_files. By choosing inline, the graphical information concerning the image is embedded into the HTML code.
3. Saving our Chart as an Image
It is also possible to save only the figure of a chart as an image file, ignoring all the code we’ve written during the session using the savefig() function which save the chart in a PNG format. See the following program:
plt.axis([0,5,0,20])
plt.title('Title goes here',fontsize=20,fontname='Times New Roman',color='red')
plt.xlabel('Counting',color='blue')
plt.ylabel('Square values',color='green')
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.text(1,1.5,'First')
plt.text(2,4.5,'Second')
plt.text(3,9.5,'Third')
plt.text(4,16.5,'Fourth')
plt.text(1.1,12,r'$y = x^2$',fontsize=20,bbox={'facecolor':'yellow','alpha':0.2})
plt.grid(True)
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.plot([1,2,3,4],[0.8,3.5,8,15],'g^')
plt.plot([1,2,3,4],[0.5,2.5,4,12],'b*')
plt.legend(['First series','Second series','Third series'],loc=2)
plt.savefig('my_chart.png')
Executing the previous code, a new file will be created in your working directory. This file will be named my_chart.png and will contain the image of your chart.
Here I am ending today’s post. Until we meet again keep practicing and learning Python, as Python is easy to learn!
could be very useful when we need to share your work on Web. Let's how to save our chart in different ways:
1. Saving the Code
If IPython is used for coding then this trick is useful in which the code concerning the
representation of a single chart is saved in a .py file using the save% command followed by the name of the file you want to save followed by the number of input prompts containing the row of code that you want to save. If all the code is written in only one prompt, as in our case, we have to add only
its number; otherwise if we want to save the code written in many prompts, for example from 10 to 20, we have to indicate this range with the two numbers separated by a -, that is, 10-20.
Suppose in our case, we would save the Python code underlying the representation of our first chart contained into the input prompt with the number 171.
In [171]: import matplotlib.pyplot as plt
...
We need to insert the following command to save the code into a new .py file.
%save my_first_chart 171
After we launch the command, we will find the my_first_chart.py file in our working directory. Later, when we open a new IPython session, we will have our chart and start to change the code at the point where we had saved it by entering the following command:
ipython qtconsole --matplotlib inline -m my_first_chart.py
Or we can reload the entire code in a single prompt in the QtConsole using the command %load.
%load my_first_chart.py
Or We can run it during a session with the command %run.
%run my_first_chart.py
2. Converting Your Session to an HTML File
Using the IPython QtConsole, we can convert all the code and graphics present in our current session to an HTML page. Simply choose File-->Save to HTML/XHTML from the menu as shown below:
As shown in the figure above we will be asked to save our session in two different formats: HTML and XHMTL. The difference between the two formats is based on the image conversion type.
If you select HTML as the output file format, the images contained in our session will be converted to PNG format. If we select XHTML as the output file format instead, the images will be converted to SVG format.
Let's our session as an HTML file and name it my_session.html, as shown in Figure below:
Next we'll be asked if you want to save your images in an external directory or inline
By choosing the external option, the images will be collected into a directory called my_session_files. By choosing inline, the graphical information concerning the image is embedded into the HTML code.
3. Saving our Chart as an Image
It is also possible to save only the figure of a chart as an image file, ignoring all the code we’ve written during the session using the savefig() function which save the chart in a PNG format. See the following program:
plt.axis([0,5,0,20])
plt.title('Title goes here',fontsize=20,fontname='Times New Roman',color='red')
plt.xlabel('Counting',color='blue')
plt.ylabel('Square values',color='green')
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.text(1,1.5,'First')
plt.text(2,4.5,'Second')
plt.text(3,9.5,'Third')
plt.text(4,16.5,'Fourth')
plt.text(1.1,12,r'$y = x^2$',fontsize=20,bbox={'facecolor':'yellow','alpha':0.2})
plt.grid(True)
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.plot([1,2,3,4],[0.8,3.5,8,15],'g^')
plt.plot([1,2,3,4],[0.5,2.5,4,12],'b*')
plt.legend(['First series','Second series','Third series'],loc=2)
plt.savefig('my_chart.png')
Executing the previous code, a new file will be created in your working directory. This file will be named my_chart.png and will contain the image of your chart.
Here I am ending today’s post. Until we meet again keep practicing and learning Python, as Python is easy to learn!
0 comments:
Post a Comment