1. Markers and line styles
plt.plot(X, Y, '<format>', ...)
controlling color:
b(blue); c(cyan); g(green); k(black); m(magenta); r(red); w(white); y(yellow).
Specifying styles in multiline plots---specifying all the lines in a single function call
plt.plot(x1, y1, fmt1, x2, y2, fmt2, ...)
Controlling line styles
'-'solid line ; '--'dashed line; '-.' dash-dot line; ':' dotted line.
controlling marker styles---several markers can be used in Matplotlb;
In the following example, we try to group up all the customization available for colors, lines, and markers in the following way:
y = np.arange(1, 3, 0.3)
plt.plot(y, 'cx--', y+1, 'mo:', y+2, 'kp-.')
2. Finer control with keyword arguments
plot() is a really rich function, and there are some keyword arguments to configure colors, markers, and line styles:
3. Handling X and Y axis
Matplotlib provides two basic functions to manage them—xticks() and yticks(). They behave in the same way, so the description for one function will apply to the other too.
4. Plot types
1) Histogram charts---Histogram charts are a graphical display of frequencies, represented as bars. They show what portion of the dataset falls into each category, usually specified as non-overlapping intervals. Matplotlib calls those categories bins.
By default, hist() uses a bin value of 10 (so only ten categories, or bars, are computed), but we can customize it, either by passing an additional parameter, for example, in hist(y, <bins>), or using the bin keyword argument as hist(y, bin=<bins>).
2) Error bar charts
Using the errorbar() function, Matplotlib allows us to create such a graph type.
plt.errorbar(x, y, yerr=e1, fmt='.-');
3) Scatter plots
A scatter plot is often used to identify potential association between two variables, and it's often drawn before working on a fitting regression function. It gives a good visual picture of the correlation, in particular for nonlinear relationships. Matplotlib provides the scatter() function to plot X versus Y unidimensional array of the same length as scatter plot.
4) Polar charts
A polar system is a two-dimensional coordinate system, where the position of a point is expressed in terms of a radius and an angle. This system is used where the relationship between two points is better expressed using those information. The angular coordinate can be expressed either in radians or in degrees. Though, Matplotlib uses degrees. The polar() Matplotlib function plots polar charts. Its parameters are two lists (of the same length)—theta for the angular coordinates and r for the radial coordinates.
There are two functions to control the radial and the angular grids: rgrid() and thetagrid() functions respectively.
theta = np.arange(0., 2., 1./180.)*np.pi
r = np.abs(np.sin(5*theta) - 2.*np.cos(theta))
plt.polar(theta, r); #1
plt.thetagrids(range(45, 360, 90)); #2
plt.rgrids(np.arange(0.2, 3.1, .7), angle=0); #3
5. Text inside figure, annotations, and arrows
1) adding text inside the figure
plt.text(x, y, text)
2) adding annotations
The annotate() function provides functionality to make annotation easy. In annotation, we have to consider two points—the graph point we want to annotate (represented by an xy keyword argument) and the plot position where we want to place the annotation text (represented by xytext). Both are expressed in an (x,y) format in data coordinate positions; Moreover, there is an additional argument to specify the arrow properties, that's the fundamental difference between text() and annotate(). We connect the annotation text to the annotated point with an arrow.
plt.plot(y);
plt.ylim(ymax=35);
plt.annotate('this spot must really\nmean something', xy=(6, 30), xytext=(8, 31.5), arrowprops=dict(facecolor='black', shrink=0.05));
Also note that we had to use ylim() to adjust the Y limits, as annotate() does not do it automatically.
3) adding an arrow
Matplotlib provides an arrow() function. It takes the coordinates of the arrow origin (x,y), and the delta distance (dx, xy), the distance at which the head is to be placed. This implies that the arrow starts at (x,y) and ends at (x + dx, y + dy)
plt.arrow(x, y, dx, dy)
Sadly, this function is quite hard to use and presents several difficulties.