[Laboratory building /matplotlib.pyplot】matplotlib Paint First Look

matplotlib to use

Introducing respective first module

from matplotlib import pyplot as plt

Plt reuse peculiar approach to drawing.

  1. Histogram --plt.bar (designed to compare the size)
  2. Histogram --plt.barh (designed to view the distribution)
  3. Pie --plt.pie (see designed to scale)
  4. Line graph --plt.plot (specifically used to draw images corresponding function)
  5. Scatter --plt.scatter
  6. FIG contour --plt.contour
    (note the distinction between the difference between the histogram and the histogram)

These methods accept two types of arrays (including ndarray type , and all can be transformed into ndarray type)
we can use numpy.linspace to generate a corresponding array of test

Custom graphics style

Is then added the appropriate mapping function in the corresponding keyword parameters, such as:

  1. color-- color
  2. linestyle-- linear style
  3. The width of the linear linewidth--
  4. alpha-- opacity
    (There are many more, but just to give the name of the keyword arguments, did not give we should give them what kind of parameters given, so I still recommend the venue to official documents)

A combination of graphic styles

There is also a drawing we need is hope to draw together different images. Of course, we plt certainly achieved those, only if we use the same content when x, matplotlib will put these figures are put together.

When a combination of graphics, you may need a more advanced operation.
That is, build their own picture of the environment.

  1. First, create a drawing board objects fig = plt.figure ()
  2. Created on a canvas object on the canvas object axes = fig.add_axes ([a, b , c, d]) - Note that a, b, c, d represent the canvas two coordinates of the lower left corner , and canvas width and height .
  3. ** Plot reuse axes of methods (or other methods) ** Paint.

Another method may also be implemented plotted subgraph.
That is to use plt.subplots ().

Then this function returns fig and axes objects (in particular axes objects),
we will be able to tailor our output

Paint specification method

We need a convention to regulate our drawing process, so that we can avoid want to do, how to do shortcomings.

A good advice is: Do not just use plt.plot to draw the image, but should use plt.subplots ways to use

· Add a chart title, legend, xy axis represents the meaning

  1. Add chart title --axes.set_title ()
  2. Adding xy axis name --axes.set_xlabel (); axes.set_ylable ();
  3. Add a legend --axes.legend ();
    (you know the legend on our axes is a separate unit , we can pass a list objects to be drawn)

· Add the axis range

Use ** set_ylim () and set_xlim () ** set coordinate axis range

Argument is a list of two elements

· Graphical notation

If we can do some noted on our graphics, then we will certainly be clearer graphics.

You have to know is that we noted on our axes, only plt.text method, which accepts three parameters:

  1. x-coordinate of the text
  2. y coordinates of the text
  3. The content of the text may be formatted using the syntax to do string c

In general we will mark the size of the column represented in the histogram, we matplotlib naturally provides a very simple way to help us achieve this demand:

bars = axes.bar(x_bar, y_bar, color='blue', label=x_bar, width=2)  # 绘制柱形图
for i, rect in enumerate(bars):
    x_text = rect.get_x()  # 获取柱形图横坐标
    y_text = rect.get_height() + 0.01  # 获取柱子的高度并增加 0.01
    plt.text(x_text, y_text, '%.1f' % y_bar[i])  # 标注文字

PS: the text says we can use pylab to use an interface similar to matlab, but in this official document is in our matplotlib discourage because it may cause some problems on the namespace.

Published 137 original articles · won praise 19 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43338695/article/details/102992788