Pyhon Data Analysis 20 - matplotlib visualized (b) the histogram

atplotlib histogram is
a histogram (bar chart), is a rectangle of length variable expression pattern of FIG statistical reports, data distribution situation by varying the height of a series of vertical stripes represent, used to compare two or more value (at different times or different conditions), only one variable, usually using the analysis to small data sets. Histogram also arranged laterally, or expressed in a multidimensional manner.

Preparation
Import numpy AS NP
Import PANDAS AS PD
from PANDAS Import Series, DataFrame
% matplotlib inline
Import matplotlib.pyplot AS PLT
. 1
2
. 3
. 4
. 5
create a data dictionary.

= Data [23 is, 45, 56 is, 78, 213]
. 1
drawing
to complete a histogram of basic codes with a. In front of the X-axis parameters, the parameter Y-axis behind.

plt.bar ([1,2,3,4,5], data)
1


Color
can adjust the color histogram.

plt.bar(range(len(data)), data, color='red')
1


Transparency
Adjust the transparency of the histogram. Note: If you adjust too much and told me the following, like, adhering to the pink feeling.


Grid
using Grid () function draws grid.

plt.bar (Range (len (Data)), Data, Color = 'RoyalBlue', Alpha = 0.7)
plt.grid (Color = '# 95a5a6', lineStyle = '-', as linewidth = 2, Axis = 'Y ', Alpha = 0.7)
. 1
2
At the same time, can set the grid color, shape, size, orientation, and transparency.


Accumulation
use the bottom property, you can set whether the accumulation of two histograms.

data1 = [23,85, 72, 43, 52]
data2 = [42, 35, 21, 16, 9]
plt.bar(range(len(data)), data1)
plt.bar(range(len(data)), data2, bottom=data1)
1
2
3
4

Operation little fun is to use the width property to convert into ordinary bar chart histograms side by side.

data1 = [23,85, 72, 43, 52]
data2 = [42, 35, 21, 16, 9]
width =0.3
plt.bar(np.arange(len(data1)), data1, width=width)
plt.bar(np.arange(len(data2))+ width, data2, width=width)
1
2
3
4
5

Using Barh () function can be drawn laterally histogram.


Scale
using xticks () function can set the histogram scale.

data = [23.85, 72, 43, 52]
plt.xticks (range (len (data)))
plt.bar (range (len (data)), data)
1
2
3


Similarly, the available xticks () function may also scale label settings.

data = [23,85, 72, 43, 52]
labels = ['A', 'B', 'C', 'D', 'E']
plt.xticks(range(len(data)),labels)
plt.bar(range(len(data)), data)
1
2
3
4

Note: You can not set the scale of individual labels, but you can set the scale alone.
Further, by yticks () function to modify the scale of the Y-axis.

Axis label
can be a function of the X-axis and Y-axis labels provided by the xlabel () and ylabel ().

data = [23,85, 72, 43, 52]
labels = ['A', 'B', 'C', 'D', 'E']
plt.xticks(range(len(data)), labels)
plt.xlabel('Class')
plt.ylabel('Amounts')
plt.bar(range(len(data)), data)
1
2
3
4
5
6


The title
by title () function sets the axis title.

data = [23,85, 72, 43, 52]
labels = ['A', 'B', 'C', 'D', 'E']
plt.xticks(range(len(data)), labels)
plt.xlabel('Class')
plt.ylabel('Amounts')
plt.title('I am title')
plt.bar(range(len(data)), data)
1
2
3
4
5
6
7


Legend
the bar () function is passed inside the legend label attribute name may be provided by Legend () function can draw the legend.

data1 = [23,85, 72, 43, 52]
data2 = [42, 35, 21, 16, 9]
width =0.3
plt.bar(np.arange(len(data1)), data1, width=width, label='one')
plt.bar(np.arange(len(data2))+ width, data2, width=width, label='two')
plt.legend()
---------------------

Guess you like

Origin www.cnblogs.com/hyhy904/p/11089231.html