Python using matplotlib to draw a bar chart tutorial

Source: Internet
Author: User
The concept of matplotlib is not much introduced here.

Small series before also share with you python using matplotlib to achieve the line chart and pie chart effect, interested friends can also click to see, below see Python use Matplotlib to draw histogram method, as follows:

1. Basic Bar Chart

Import Matplotlib.pyplot as Pltdata = [5, +, +, 10]plt.bar (range (len data), data) Plt.show ()


The Plt.bar function signature is:

Bar (left, height, width=0.8, Bottom=none, **kwargs)

In fact, the Left,height,width,bottom four parameters determine the position and size of the cylinder. By default, left is the center position of the cylinder (you can change the meaning of the left value by the align parameter), which is:

    • (left - width / 2, bottom)To the lower left corner position

    • (left + width / 2, bottom + height)To the upper right corner position

For example:

Import Matplotlib.pyplot as Pltdata = [5, 0, 6,, 10]plt.bar ([0.3, 1.7, 4,, 7], data, width=0.6, bottom=[10, 5, 0 , 5]) plt.show ()


2. Set the bar style

(1) Color

The column color can be set by the Facecolor (or FC) keyword parameter, for example:

Import Matplotlib.pyplot as Pltdata = [5, +, +, 10]plt.bar (range (len data), data, fc= ' G ') plt.show ()


You can set multiple colors at once by using the Color keyword parameter, for example:

Import Matplotlib.pyplot as Pltdata = [5, +, +, 10]plt.bar (range (len (data), data, color= ' RGB ') # or ' color=[' r ', ' G ', ' B '] ' plt.show ()


(2) Stroke

The relevant keyword parameters are:

    • Edgecolor or EC

    • LineStyle or LS

    • LineWidth or LW

For example:

Import Matplotlib.pyplot as Pltdata = [5, +, +, 10]plt.bar (len (data), data, ec= ' R ', ls= '--', lw=2) plt.show ()


(3) Filling

The Hatch keyword can be used to set the fill style, with the following values:/, \, |,-, +, X, O, O,., *. For example:

Import Matplotlib.pyplot as Pltdata = [5, +, +, 10]plt.bar (len (data), data, ec= ' K ', lw=1, hatch= ' O ') plt.show ( )

3. Set the Tick label

Import Matplotlib.pyplot as Pltdata = [5, +, +, 10]labels = [' Tom ', ' Dick ', ' Harry ', ' Slim ', ' Jim ']plt.bar (len (data)), data, Tick_label=labels) Plt.show ()


4. Stacked bar Chart

With the bottom parameter, you can draw a stacked bar chart. For example:

Import NumPy as Npimport matplotlib.pyplot as Pltsize = 5x = Np.arange (size) A = Np.random.random (size) b = np.random.random (size) Plt.bar (x, A, label= ' a ') Plt.bar (x, B, Bottom=a, label= ' B ') plt.legend () plt.show ()


5. Parallel Column Chart

Drawing a parallel histogram is similar to a stacked histogram, which is to draw multiple groups of cylinders, just to control the position and size of each group of cylinders. For example:

Import NumPy as Npimport matplotlib.pyplot as Pltsize = 5x = Np.arange (size) A = Np.random.random (size) b = np.random.random (size) c = np.random.random (size) total_width, n = 0.8, 3width = Total_width/nx = X-(total_width-width)/2plt.bar (x, A, width=width, label= ' a ') Plt.bar (x + width, b, width=width, label= ' B ') Plt.bar (x + 2 * width, C, width=width, label= ' c ') pl T.legend () plt.show ()


6. Bar chart

Use the Barh method to draw a bar chart. For example:

Import Matplotlib.pyplot as Pltdata = [5, +, +, 10]plt.barh (range (len data), data) Plt.show ()


The signature of the Plt.barh method is:

Barh (bottom, Width, height=0.8, Left=none, **kwargs)

You can see similar to the Plt.bar method. Therefore, the drawing of stacked bar charts and parallel bars is similar to the previous, not to repeat.

7. Positive and Negative bar chart

Import NumPy as Npimport matplotlib.pyplot as Plta = Np.array ([5, 5, +, +]) b = Np.array ([[Ten],,]) Plt.bar H (Range (Len (a)), a) Plt.barh (range (len (b)),-B) plt.show ()

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.