Use python's matplotlib library to draw bar graphs, line graphs, and pie charts

The Excel in the office component we usually use has a relatively powerful drawing function, and can carry out a variety of images, such as bar graphs, line graphs, pie graphs, and even scatter graphs. It can even be used for text documents. Some simple images. But the images they draw are relatively fixed and relatively small in function.

Today we will introduce matplotlib, an important plotting library in python.
The matplotlib library is a third-party library of python. If you install IDLE of python, you need to install it additionally. Generally, what we install is that Anaconda (python integrated development environment) is already built-in and no additional installation is required.

  1. Histogram
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #显示中文
x=[i for i in range(1,6)]
y=[2,4,3,6,7]
plt.bar(x,y,color='r',width=0.3)
plt.show()

The effect is shown in the figure.
Insert picture description here
This is the simplest histogram, and you can even remove the third row.

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #显示中文
y=[2,4,3,6,7]
plt.bar(x,y,color='r',width=0.3)
plt.show()

The effect is the same, the abscissa will be created by default at this time

  • Tagging
plt.xlabel('得分')
plt.ylabel('人数')
  • Caption
plt.title('成绩分析图')
  • Custom abscissa label
heng=['小明','小红','小刚','小芳','小雨']
plt.bar(x,y,color='r',tick_label=heng,width=0.3)
  • Rotate histogram direction
plt.barh(x,y,color='r',tick_label=hen,height=0.3)
  • Save the picture to the same local directory
plt.savefig('picture.png')

To increase the clarity of the picture, you can set the dpi size

plt.savefig('picture.png',dpi=300)
  • Complete code
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] 

x=[i for i in range(1,6)]
y=[2,4,3,6,7]
heng=['小明','小红','小刚','小芳','小雨']
plt.bar(x,y,color='r',tick_label=heng,width=0.3)
plt.xlabel('同学')
plt.ylabel('分数')
plt.title('成绩分析图')
plt.savefig('picture.png')
plt.show()

Insert picture description here
The core code is only one sentence

plt.bar(x,y,color='r',tick_label=heng,width=0.3)
  1. line chart
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] 
y1=[1127968,1193973  ,1330385  ,1211292 , 1365868]

y2=[950600 , 967387  ,1102495 , 992571  ,1132938]

y3=[ 797236 , 872468  ,922418,  896010  ,950669]

y4=[405868  ,540142 , 682967  ,774129  ,789672]
x=[1,2,3,4,5]
plt.plot(x,y1,'r*--',linewidth=2.5,label='会员A')
plt.plot(x,y2,'b*--',linewidth=2.5,label='会员B')
plt.plot(x,y3,'g*--',linewidth=2.5,label='会员C')
plt.plot(x,y4,'y*--',linewidth=2.5,label='会员D')
plt.xlabel('天数')
plt.ylabel('伤害')
plt.legend()
plt.title('会员们的伤害变化')
plt.show()

Insert picture description here

The thickness of the control line becomes linewidth.

  • Add legend
plt.plot(x,y1,'r*--',linewidth=2.5,label='会员A')
plt.legend()

Among them,'r*–','r' represents color,'*' represents, and'- -'represents the curve style is dashed line.

  • colour
letter Corresponding color
r red
b blue
g green
and yellow
k black
w white
m purple
c Blue

In fact, red, green, yellow, etc. can also be used to represent colors.

  1. Discrete data point type
symbol Means
. Black spot
+ plus
* Asterisk
The Solid dot
d diamond
p Five-pointed star
h Six-pointed star
x X number
s Cube
  1. Continuous line
symbol Means
- solid line
- - Dashed line
dotted line
-. Dotted line
  1. Pie chart
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] 
d={
    
    '美国': 1368036, '英国':223060, '法国':176970, '巴西': 163510, '意大利':219070, 
   '俄罗斯':221344, '德国': 171999, '西班牙':268143, '土耳其':138657, '伊朗':109286,
   '其他':1069769}
a = sorted(d.items(), key=lambda x: x[1],reverse=True)
a=dict(a)   #处理数据
size=a.values()    #提取出数字
color=['red','blue','green','pink','orange','purple','yellow']   #分配颜色,若颜色不够,会循环已有的颜色
label=a.keys()   #提取出国家
explode=(0.05,0,0,0,0,0,0,0,0,0,0)
pie=plt.pie(size,explode=explode,colors=color,labels=label,
        autopct='%1.1f%%',startangle=90,counterclock=False)
        #核心代码
plt.show()

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_46530492/article/details/108230234