Python uses matplotlib to draw a bar chart tutorial, pythonmatplotlib

Source: Internet
Author: User
Tags install matplotlib

Python uses matplotlib to draw a bar chart tutorial, pythonmatplotlib

Matplotlib is not described here.How to install Matplotlib In the Drawing Library:Click here

I have also shared with you the line chart and pie chart effects achieved by using matplotlib in python before. If you are interested, you can click to view the results. Let's take a look at how python uses matplotlib to draw a bar chart, the details are as follows:

1. Basic bar chart

import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 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, and bottom parameters determine the position and size of the column. By default, left is the center position of the column (you can use the align parameter to change the meaning of the left value), that is:

  • (left - width / 2, bottom)In the lower left corner
  • (left + width / 2, bottom + height)In the upper-right corner

For example:

import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 10]plt.bar([0.3, 1.7, 4, 6, 7], data, width=0.6, bottom=[10, 0, 5, 0, 5])plt.show()


2. Set the bar style

(1) color

You can use the facecolor (or fc) keyword parameter to set the color of the bar, for example:

import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 10]plt.bar(range(len(data)), data, fc='g')plt.show()


You can use the color keyword to set multiple colors at a time. For example:

import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 10]plt.bar(range(len(data)), data, color='rgb') # or `color=['r', 'g', 'b']`plt.show()


(2) stroke

The related keyword parameters are:

  • Edgecolor or ec
  • Linestyle or ls
  • Linewidth or lw

For example:

import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 10]plt.bar(range(len(data)), data, ec='r', ls='--', lw=2)plt.show()


(3) Filling

The hatch keyword can be used to set the fill style. The optional values are:/, \, |,-, +, x, o, O ,.,*. For example:

import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 10]plt.bar(range(len(data)), data, ec='k', lw=1, hatch='o')plt.show()

 

3. Set tick label

import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 10]labels = ['Tom', 'Dick', 'Harry', 'Slim', 'Jim']plt.bar(range(len(data)), data, tick_label=labels)plt.show()


4. Stacked column chart

You can use the bottom parameter to draw a Stacked column 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

Similar to the Stacked column chart, a parallel column chart is used to draw multiple groups of bars. You only need to control the position and size of each group of bars. 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')plt.legend()plt.show()


6. Bar Chart

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

import matplotlib.pyplot as pltdata = [5, 20, 15, 25, 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)

The method is similar to that of plt. bar. Therefore, the stacked bar chart and parallel bar chart are similar to the preceding method.

7. Positive and Negative bar chart

import numpy as npimport matplotlib.pyplot as plta = np.array([5, 20, 15, 25, 10])b = np.array([10, 15, 20, 15, 5])plt.barh(range(len(a)), a)plt.barh(range(len(b)), -b)plt.show()

Summary

The above is all about this article. I hope this article will help you learn or use python. If you have any questions, please leave a message.

Related Article

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.