Tutorial on creating a line chart using matplotlib in python

Source: Internet
Author: User
Tags install matplotlib
Matplotlib is a Python toolbox for data visualization in scientific computing. With it, Python can plot a variety of data graphics, such as Matlab and Ave. The following article describes how to use matplotlib to draw a line chart in python. For more information, see. Matplotlib is a Python toolbox for data visualization in scientific computing. With it, Python can plot a variety of data graphics, such as Matlab and Ave. The following article describes how to use matplotlib to draw a line chart in python. For more information, see.

Introduction to matplotlib

Matplotlib is the most famous plotting library in python. It provides a complete set of command APIs similar to matlab and is very suitable for interactive plotting. It can also be easily used as a drawing control and embedded into GUI applications.

Its documentation is quite complete, and there are hundreds of thumbnails on the Gallery page. after opening it, all the source programs are available. Therefore, if you want to draw a certain type of graph, you can simply browse, copy, and paste the graph on this page.

In Linux, the famous data graph tool also has gnuplot, which is free of charge. Python has a package that can call gnuplot, but the syntax is not used to it and the drawing quality is not high.

Matplotlib is relatively strong: Matlab syntax, python language, latex drawing quality (you can also use the built-in latex engine to draw mathematical formulas ).

How to install Matplotlib in the drawing library:Click here

Matplotlib

1. line chart

import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0, 2 * np.pi, 100)y1, y2 = np.sin(x), np.cos(x)plt.plot(x, y1)plt.plot(x, y2)plt.title('line chart')plt.xlabel('x')plt.ylabel('y')plt.show()

2. Legend

Specify the label during the plot, and then call the legend method to draw the legend. For example:

import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0, 2 * np.pi, 100)y1, y2 = np.sin(x), np.cos(x)plt.plot(x, y1, label='y = sin(x)')plt.plot(x, y2, label='y = cos(x)')plt.legend()plt.show()

(2) style

The keyword parameter linestyle (or ls) of the plot method is used to set the line style. Optional values:

  • -, Solid

  • --, Dashed

  • -., Dashdot

  • :, Dotted

  • '','', None

(3) width

You can set the keyword parameter linewidth (or lw) of the plot method to change the line width. its value is a floating point number.

import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0, 2 * np.pi, 100)y1, y2 = np.sin(x), np.cos(x)plt.plot(x, y1, c='r', ls='--', lw=3)plt.plot(x, y2, c='#526922', ls='-.')plt.show()

4. marker

The following keyword parameters can be used to set the marker style:

  • Marker

  • Markeredgecolor or mec

  • Markeredgewidth or mew

  • Markerfacecolor or mfc

  • Markerfacecoloralt or mfcalt

  • Markersize or MS

The value of marker is:

  • '.': Point marker

  • ',': Pixel marker

  • 'O': circle marker

  • 'V': triangle_down marker

  • Pai_^ _ ^ ': triangle_up marker

  • '<': Triangle_left marker

  • '>': Triangle_right marker

  • '1': tri_down marker

  • '2': tri_up marker

  • '3': tri_left marker

  • '4': tri_right marker

  • 'S ': square marker

  • 'P': pentagon marker

  • '*': Star marker

  • 'H': hexagon1 marker

  • 'H': hexagon2 marker

  • '+': Plus marker

  • 'X': x marker

  • 'D': diamond marker

  • 'D': thin_diamond marker

  • '|': Vline marker

  • '_': Hline marker

For example:

import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0, 2 * np.pi, 10)y1, y2 = np.sin(x), np.cos(x)plt.plot(x, y1, marker='o', mec='r', mfc='w')plt.plot(x, y2, marker='*', ms=10)plt.show()

In addition, the marker keyword parameter can be combined with the color and linestyle keywords into a string. For example:

import numpy as npimport matplotlib.pyplot as pltx = np.linspace(0, 2 * np.pi, 10)y1, y2 = np.sin(x), np.cos(x)plt.plot(x, y1, 'ro-')plt.plot(x, y2, 'g*:', ms=10)plt.show()

For more articles about how to use matplotlib to draw a line chart in python, refer to PHP!

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.