Drawing charts in Python

Source: Internet
Author: User

Drawing charts in Python

 

 

----------------

Environment:
Win 10. python3.5

-----------------

Preface:

I was most impressed with some java GUI programming processes:
1) All methods related to 'paint' should be called before the show () method is called.

2) in java, a drawing-Related Object (such as JFrame) will be inherited/called to process 'paint' work.

3) GUI programs in java are implemented by nested panel layers.

These three little programming experiences will be helpful for creating python charts.

 

For Chinese cabbage in scientific research, plotting is often the focus of experimental reports. Therefore, many people will like matlab, a convenient language such as python, as a mathematical and scientific processing tool. I was born from software development. The first language I came into contact with was the object-oriented language-java. The first time I came into contact with python, I have not understood the routines. I hope to use this blog post as my study notes.

Not to mention. First:

This is what we want to achieve.

 

First, let's take a look at the effect of a default plt object calling the show () method: (2) a drawing-Related Object (such as JFrame) will be inherited/called in java) to process 'paint', The plt in python can also be seen as an object. It helps us implement a lot of details when calling show (), such as the Title of this program)

 

 

 

The following describes how to draw images.

 

Import the required package:
Matplotlib is the tool used for plotting. Many methods in this tool are similar to matlab.

If we can, we still need to scientifically process the data. Then numpy will also be imported.

At the same time, in order to save the file, we need to call some operating system-level commands, so we will also use the OS

So first:

 

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

 

After the import, assume that we already have data:

Xdata = [512, 1536, 2048, 3072, 4096, 4608, 5632, 6144, 7168, 8192, 9216, 9728, 10752, 11776, 12800, 14336, 15360,
17408, 27990]
Ydata = [1.1285, 1.5314, 1.2771, 0.9415, 0.7562, 0.6966, 0.597, 0.5559, 0.5, 0.4706, 0.4302, 0.4123, 0.3807, 0.3559,
0.3419,
0.3147, 0.299, 0.2741, 0.2008]
 
 

At the same time, we also know some basic functions of plt:

Plt. clf () clears the current chart
Plt. title ('1') set the title to 1 for the chart
Plt. xlabel ('samples') sets the x-axis name in the chart as samples.
Plt. ylabel ('oss value') sets the unit name of the Y axis in the chart.
Plt. ylim (ymin = 0) sets the minimum value of y.
Plt. ylim (ymax = 2) sets the maximum value of the Y axis.
Plt. savefig (fname) saves the image. fname is the absolute path of the chart.
Plt. show () shows the current image.
It is not enough to know these methods, and the maximum effect can be achieved. After all, we still don't know how the data is 'painted on the fig.

 

This effect does not seem to be good enough. We hope to have a reference line on the graph plt:

Plt. grid (True, linestyle = '-', color = '0. 75 ') # The phrase should be called before the show () method is called, because: all methods related to 'paint' should be called before the show () method is called.
 
 

 

With this, we 'd better draw a point:

 

Plt. scatter (x, y, marker = '*') # convert (x [I], y [I]) in the array x [], y [I]) draw a coordinate point on the icon as a coordinate point, and use the * sign to mark the point.
Or
Plt. plot (x, y, 'R') # convert (x [I], y [I]) in the array of x [], y [I]) draw a coordinate point on the icon and connect it with a red line.
As mentioned above, there are also many default parameters that can be specified. Plt. plot (x, y, linestyle = lineStyles [0], linewidth = 1.5, c = colors [0]) # plot point
 
These two statements can be called together:
plt.scatter(x, y, marker ='*')
plt.plot(x, y, linestyle=lineStyles[0], linewidth=1.5, c=colors[0])  # plot point
The effect is as follows:
Have you found that the previously drawn red lines are covered by green lines? This is very similar (GUI programs in java are implemented by layer-by-layer panel nesting .)
 

When you are careful, you will surely find that there is a line sign in the upper right corner of the chart.
plt.legend(["d=%i" %m for m in [1,2,3]], loc="upper left")
# In this statement, the icon of each chart is placed in the upper right corner, and the name of each chart is 1 2 3.
 
 
 

 

Finally:

 

Plt. savefig (fname) # Save the image
 
You can also use
Plt. xticks (x) x is the vector of the grid lines to be drawn.
Plt. yticks (y) y is the y-axis vector of the grid line to be drawn.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.