What do the dots mean in Python
matplotlib - a plotter for diagrams¶
Adaptation of matplotlib diagrams¶
Matplotlib diagrams generally have the following structure:
The basis of every diagram is therefore an object (with a possible title), the actual diagram is determined by the value range of the axes (). In particular, a distinction must be made between and: The latter designation only refers to either the - or the -Axis.
Using the sine plot from the last section, the following example shows how the above objects can be used to adapt or improve the appearance of a Matplotlib diagram. 
With the above plot instruction you get - without any further settings - the following diagram:
In this way, you can get an idea of a mathematical function with very little effort. The fact that only a minimal amount of code is required is also due to the fact that standard values are predefined for many settings in the Matplotlib functions. If you were to specify all of these values explicitly, the code example above would look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
# Import modulesimportnumpyasnpimportmatplotlib.pyplotasplt # Create value lists for a sine and cosine function: x = np.linspace (-np.pi, np.pi, 500, endpoint = True) cos_x = np.cos (x) sin_x = np.sin (x) # Create a new Matplot Figure object with 8x6 inches and # a resolution of 100 dpi: plt.figure (figsize = (8,6), dpi = 80) # In this figure a 1x1 diagram - Create grid; # The first of this grid is selected as the current diagram: plt.subplot (111) # Cosine function with blue color, continuous line and 1 pixel # Plot line width: plt.plot (x, cos_x, color = "blue ", linewidth = 1.0, linestyle =" - ") # sine function with green color, continuous line and 1 pixel # plot line width: plt.plot (x, sin_x, color =" green ", linewidth = 1.0, linestyle =" - ") # Define limits for the x-axis: plt.xlim (-4.0,4.0) # Define limits for the y-axis: plt.ylim (-1.0,1.0) #" Ticks "(reference points) for the x-axis define: plt.xticks (np.linspace (-4,4,9, endpoint = True)) # "Ticks" (reference points) for y-axis set: plt.yticks (np.linspace (-1,1,5, endpoint = True)) # show diagram: plt.show ()
The output of this code is absolutely identical to the diagram above. However, this “more detailed” code example can be used very well as a starting point for various adaptations.
Adjust size, colors and borders
The original diagram appears at the original size ( Inches) strongly stretched in the vertical direction; in the case of the sine and cosine functions, their range of values is only between and a wider diagram is probably more suitable. To do this, you can replace the line in the code example above with the following line:
The lines are also shown in the original diagram with a line width of only printed. If you were to print this diagram with a black-and-white printer, for example (and then possibly copy the printed page), the lines would no longer be easy to see - the lines would also have similar gray values. To improve this, you can replace the lines and in the code example above with the following lines:
There is also no value range for the - and -Axis, the matplotlib simply uses the minima and maxima of the values to be displayed as diagram limits (rounded up to the next larger integer value); the diagram line therefore touches the diagram boundaries. If you want a "softer" transition here, i.e. not adjoining diagram lines, a slightly larger range of values can be selected for the diagram limits. To do this, you can replace the lines and in the code example above with the following lines:
With these adjustments you get the following diagram when you call up:
Adjust "Ticks" (reference points) for the axes:
The usual scaling is used to represent the trigonometric functions -Axis not ideal: For example, you can only read approximately at which points the functions have zeros or maxima. It would be more practical to scale the -Axis based on the number of circles to be determined. To do this, you can replace the line in the code example above with the following line:
If you plot the modified diagram, you get on the Axis shows the numerical values, for example instead of . Fortunately, when you call up you can not only define the position of the ticks, but also specify their labeling by specifying a second list, and optionally also use LaTeX formula symbols for this:
This in front of the individual character strings means that they are viewed as, i.e. special characters such as do not have to be provided with an additional character.
In the example above, the labeling of the -Axis adjusted so that the font is identical. When you call up the result you get the following diagram:
Move diagram axes:
The axis object of a chart has one property called; it defines where the axes are to be displayed. By default, the - Axis printed on the left edge of the chart. This has the advantage that you can also use the right edge for the additional plotting of a second curve as a value axis, provided that the value ranges of the two lines differ greatly from one another.
However, if a coordinate system with four quadrants is to be drawn, for example for discussion of curves or geometric tasks, there is sometimes a diagram with a zero point in the middle and two running through it - respectively -Axes more suitable. To do this, the axes must be "shifted" to the center. This can be achieved by removing two of the four (left, right, top, bottom) and moving the other two to the center. To do this, you can insert the following lines in front of the instruction in the code example above:
The result is the following diagram when you call up:
This form of representation is elegant, but in this case the lettering of the - and -Axis partially covered by the function graph. As a workaround, you can on the one hand change the font size of the axis labeling and on the other hand highlight it with a semi-transparent border. To do this, you can insert the following code in front of the instruction:
The result is obtained by calling:
Sine and cosine plot with different positioning of the axes and highlighted axis lettering.
Add title, legend and text
Normally, diagrams are included in the text with a caption ("Caption"); In most cases, a figure number is also inserted so that the diagram can be clearly referenced from the text. If, on the other hand, you create a diagram for a pin board or an overhead slide / projector presentation, a large printed title above the diagram is sometimes useful. One of these can be added to the diagram as follows:
Here, too, LaTeX code can be used in the title text if necessary.
As a further improvement, it is very useful if the meaning of the individual lines is displayed as a "legend" in a diagram with several lines. To do this, you can also specify a parameter for the individual instructions and then display the legend using:
Additional text elements can be added to the diagram as additional labeling. Special points can also be highlighted with arrows or auxiliary lines.
This finally gives the following diagram:
... to be continued ...
- How many people here are teenagers
- Is grapeseed oil healthy for you
- What's going on in Eritrea
- Chinese Indonesians still experience racism today
- What is your favorite day 1
- What is the legality of homemade weapons
- Can dengue fever delay menstruation
- I ate raw chicken, it's that bad
- What famous people are playing Dota 2
- Is it difficult to stop playing?
- Where do the airlines get the entry fee from?
- What does house cleaning include
- How can I become an affiliate marketer
- How do I find an email address
- How do I cause a power failure
- What pennies from the 1950s are valuable
- What are Paid Media Tactics
- What is the richest city in Myanmar
- What are the best B2B marketing agencies
- Why are you still single
- Is this evening dress formally flashy enough?
- Why is Mecca a sacred place
- Is cancer really preventable
- What does a mass diet look like
- How fast food and obesity are related
- Is computer software patentable
- Is a conscious fetus
- When are we going to colonize Mars?
- Could AI ever win the imagination?
- Expires whey protein powder
- Which city should you never go to?
- What is an ecommerce software application
- What are the roles of the school
- Are private high schools worth the investment?