Review Review:
Python provides a number of excellent packages for data presentation, including the matplotlib module, which makes it easy to create high-quality data packages such as line graphs, bar charts, scatter plots, and so on.
Regarding the matplotlib module, we have already learned the basic framework of matplotlib, as well as common methods.
matplotlib module basics: Initial knowledge of the matplotlib module and learning of common methods
Matplotlib Module Underlying Principles: The matplotlib module consists of three layers of details: the scripting layer, the artwork layer, and the back-end layer.
matplotlib module line plotting: Summarize the properties and methods associated with line graphs
Among the charts provided by the matplotlib module, besides line charts, which are the most used, bar charts are also the charts we use for daily data analysis.
Next, we'll start learning about the properties and methods associated with plotting bar charts.
1. Overview of bar charts
1.1 What is a bar chart
- A bar chart, also known as a bar graph, is a chart that uses the length of a rectangle as the variable data for statistical purposes
- Bar graphs are used to compare two or more types of
- There is only one bar graph with the length of the rectangle as the variable
- Bar charts can be displayed horizontally or in a multi-dimensional manner.
1.2 Scenarios for using bar charts
- Bar charts are suitable for analysis of smaller data sets
- For two-dimensional datasets, comparing only one dimension of data difference terms
- Visualize differences in data between individuals
- Expression of discrete time series
1.3 Steps in drawing a bar chart
- import module
- Prepare the data, you can use numpy/pandas to organize the data
- Calling () to draw a bar chart
1.3 Case presentation
This time, we analyze the annual sales of products within the last 5 years to show the
The data used for the cases are as follows:
import random x_data = ["20{}surname Nian".format(i) for i in range(16,21)] y_data = [(100,300) for i in range(6)]
Plotting bar charts
import as plt ["-serif"]=['SimHei'] ["axes.unicode_minus"]=False for i in range(len(x_data)): (x_data[i],y_data[i]) ("Volume Analysis") ("Year") ("Sales.") ()
2. Bar chart properties
2.1 Column color filling
- facecolor (fc) keyword
- color Keywords
- Color abbreviation:
attribute value | clarification | attribute value | clarification |
---|---|---|---|
"b"/"bule" | blue (color) | "m"/"magenta" | magenta |
"g" /"green" | greener | "y"/"yellow" | yellow (color) |
"r"/"red" | red (color) | "k"/"black" | ferrous |
"c"/"cyan" | blue-green | "w"/"white" |
- Format form: (r,g,b)
- Value range: 0~1
2.2 Shape tracing settings
Column Border Color
- edgecolor or ec
Column Border Styles
- linestyle or ls
- Line Style:
attribute value | clarification |
---|---|
"-" 、"solid" | Default solid line display |
"--"、"dashed" | dotted line |
"-." "dashdot" | dotted line |
":"、"dotted" | dotted line |
"None" """" | unoccupied |
2.3 Width of shape border
- linewidth or lw
- Bar Chart Fill Styles
hatch: sets the fill style
Attribute values: {'/', '', '|', '-', '+', 'x', 'o', 'O', '.' , '*'} |
2.4 Scale labels
- tickle label: defaults to numeric labels
We add a border style of "--" to the first bar chart, add the specified rgb color, and fill the circle.
for i in range(len(x_data)): (x_data[i],y_data[i],color=(0.2*i,0.2*i,0.2*i),linestyle="--",hatch="o")
3. Stacked bar charts
- In a bar chart, we will be comparing two sets of data in the same category at the same time, so we need to draw a stacked bar chart
- bottom : the y-coordinate of the base of the bar, default value is 0
In the first section of the case, add a set of y-axis data all data as follows:
x_data = ["20{}surname Nian".format(i) for i in range(16,21)] y_data = list((100,300) for i in range(5)) y2_data = list((100,300) for i in range(5))
Add the method one more time, adding the bottom attribute
(x_data,y_data,lw=0.5,fc="r",label="Phone") (x_data,y2_data,lw=0.5,fc="b",label="Android",bottom=y_data)
4. Side-by-side bar charts
To control the position and size of each bar in a side-by-side bar graph you can use the width attribute
- width: set the width of each group of bars
- x-axis: the width of the x-axis should be set directly for each group as well
For example, to continue the transformation of the above case, we added the width attribute to bar1 and bar2, and then individually set the width of the x-axis side by side to 0.3
x_width = range(0,len(x_data)) x2_width = [i+0.3 for i in x_width] (x_width,y_data,lw=0.5,fc="r",width=0.3,label="Phone") (x2_width,y2_data,lw=0.5,fc="b",width=0.3,label="Android") (range(0,5),x_data)
5. Horizontal bar charts
In the bar chart, sometimes you need to let the bar chart is placed horizontally to compare the difference, we need to use to thebarh
methodologies
- (y,width): draw a horizontal bar graph
- In conjunction with the above case, the barh method is used instead
x_data = ["20{}surname Nian".format(i) for i in range(16,21)] y_data = list((100,300) for i in range(5)) y2_data = list((100,300) for i in range(5)) x_width = range(0,len(x_data)) x2_width = [i+0.3 for i in x_width] (x_width,y_data,lw=0.5,fc="r",height=0.3,label="Phone") (x2_width,y2_data,lw=0.5,fc="b",height=0.3,label="Android") (range(0,5),x_data) () ("Volume Analysis") ("Year") ("Sales.") ()
6. Adding line bar charts
When we look at bar charts, we sometimes need to view them with the aid of a folded line
- utilization
()
Summary Methodology Line Chart - simultaneous use
()
Display coordinate values - When stacking diagrams, you need to calculate the relative positions of the fold lines
(x_data,y_data,color="pink",linestyle="--") (x_data, y2_data+200, color="skyblue", linestyle="-.") # Histograms (x_data,y_data,lw=0.5,fc="r",width=0.3,label="Phone",alpha=0.5) (x_data,y2_data, lw=0.5, fc="b", width=0.3, label="Android",alpha=0.5,bottom=y_data) for i,j in zip(x_data,y_data): (i,j+0.05,"%d"%j,ha="center",va="bottom") for i2,j2 in zip(x_data,y2_data): (i2,j2+180,"%d"%j2,ha="center",va="bottom")
7. Positive and negative bar charts
We need to use the Axes object to set the position of the axes
- first using
()
method creates the axes object - Then use
The module calls set_position to set the position of the axes.
- set_position Sets the axis position point
- The spines[] option has "
left"|"bottom"|"width"|"height"
- set_position value format is (position type, quantity); position type; "outward"|"axes"|"data"|; quantity: center->("axes",0.5), zero->("data",0.0)
y_data = (100, 300,5) y2_data = (100, 300,5) ax = () ["bottom"].set_position(('data', 0)) (x_data,+y_data,lw=0.5,fc="r",width=0.3,label="Phone") (x_data,-y2_data, lw=0.5, fc="b", width=0.3, label="Android") for i,j in zip(x_data,y_data): (i,j,"%d"%j,ha="center",va="top") for i2,j2 in zip(x_data,y2_data): (i2,-j2,"%d"%j2,ha="center",va="bottom")
Summary.
In this paper, we atplotlib module to learn in detail to draw a variety of bar chart markers related properties and methods, in the encounter need to visualize the difference between the discrete data points, we can use bar () or barh () to draw a beautiful chart.
To this point this article on Python with matplotlib plotting histograms of the article is introduced to this, more related matplotlib plotting histogram content please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!