A little bit of caution about plotting python data

  关于python数据作图的一点小小心得,

Reasons for writing this blog:

A little bit of caution about python data mapping. As a computer novice, I have to learn all kinds of software operations every day in class, and I am very upset. The computer is full of all kinds of things only when I do homework. Open the application, as a patient with severe obsessive-compulsive disorder, I am very unhappy, so I decided to use python instead of pass for drawing. As we all know, Python has powerful data processing functions. It must be a trivial matter to draw based on data;
nonsense Let’s not say more,
let’s start directly below.

  • Bar graph
  • Pie chart
  • Donut chart
  • Histogram
  • Stem and leaf diagram
  • Box diagram
  • Scatter plot
  • Radar chart

In my limited knowledge memory, I feel that scatter plots and contour plots are useless, so I won't introduce them. We start to get into the topic

Bar graph

.用宽度相同的条形来表示数据的多少的图形,用来观察不同类别频数的多少货分布状况

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

weights = np.array([90,60,80,80,100,70,80,90,100,60,
70,100,80,90,80,70,60,80,60,60,
100,70,90,80,80,100,60,70,80,90,
90,100,80,70,60,100,90,70,70,80,
80,90,80,60,70,90,80,80,90,80,
70,100,80,90,80,70,60,80,60,90,
90,60,80,80,100,70,80,90,100,60,
90,100,80,70,60,100,90,70,70,80,
100,70,90,80,80,100,60,70,80,90,
80,90,80,60,70,90,80,80,90,80
])
sections = [50,60,70,80,90,100 ]
group_names = [60,70,80,90,100]
cuts = pd.cut(weights,sections,labels=group_names)
counts = pd.value_counts(cuts)
dict(counts)
cuts.value_counts().plot(kind='bar')
plt.show()

The following is the code running effect.
Insert picture description here
When the amount of data is not large, you can consider manual input, but if the amount of data is huge, you can try to import it from excel. The principle is the same as the above.

Pie chart
用圆形及扇形的角度来表示数值大小,主要用来表示一个样本(或总体)中类别的频数占
全部频数的比例,对于研究结构性问题巨有用
import matplotlib.pyplot as plt
labels =['A', 'B', 'C', 'D', 'E']
sizes=[14,21,32,18,15]
colors=['yellow', 'green', 'gold', 'blue', 'lightcoral']
explode =(0,0.1,0,0,0)
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.1f%%',
        shadow=True,startangle=90)
plt.axis('equal')
plt.show()

The 0.1 in the explode is to highlight the green part, and adjust the amount of highlighting according to your needs. The
specific effect. The
Insert picture description here
ring chart is similar to the pie chart, which is a pie chart with a pie chart inside.

Histogram

A common graph used to display the distribution of quantitative data uses the width and width (ie area) of a rectangle to represent the frequency distribution

The following explains the difference between histogram and bar graph.
(1) 条形图中的每一个矩形表示一个类别,其宽度没有意义,而直方图当中宽度表示各组的距距
(2)直方图当中分组的数据具有连续性,条形图则是分开排列的
(3)条形图用来定性数据,而直方图则用来定量展示数据
Below is the implementation code to import data through Excel.

import os
from numpy import array
import numpy as  np
import  pylab as pl
import pandas as pd
import matplotlib.pyplot as plt

oriPath ="D:/program files/tuxin/demo/data/lightlive.csv"
def get_data(lines):
    sizeArray=[]
    for line in lines:
        line =line.replace("\n","")
        line = line.split(",")

        if line[-1] == '':
            line = line[: -1]
        line = [int(i) for i in line]
        sizeArray += line
    return  array(sizeArray)
f=open(oriPath, 'rt')
Lenths =get_data(f.readlines())
def draw_hist(lenths):
    data=lenths
    print(data.min())
    _min = data.min()
    _min = _min - (_min%10)
    bins=np.linspace(_min,data.max(),10)
    bins = [int(i) - (int(i)%10) for i in bins]
    print(bins)

    pl.hist(data,bins, density=True)
    pl.xlabel("Number of xxx")
    pl.ylabel("Number of occurences")
    pl.title("Frequence distribution of number of lightlive")
    pl.show()
draw_hist(Lenths)

Insert picture description here

Stem and leaf diagram

The idea of ​​the stem-leaf diagram is to compare the numbers in the array by digits, and use the digits with the same or little change as a main stem (stem), and the digits with a large change as the branch (leaf). , Listed behind the trunk, so you can clearly see the numbers behind each trunk, and how much each number is

We have learned and used the stem and leaf diagram since junior high school, so this is also a relatively simple data graph, so I won’t introduce it. If you’re interested, you can go and learn about it briefly.

Box diagram

Also known as box and whisker chart, box chart or box plot, it is a statistical chart used to display a set of data scattered. It is named after its shape like a box. It is also often used in various fields, often in quality management. It is mainly used to reflect the characteristics of the original data distribution, and it can also compare the distribution characteristics of multiple sets of data. The method of drawing a box plot is: first find the maximum, minimum, median, and two quartiles of a set of data; then, connect the two quartiles to draw the box; then combine the maximum and The minimum is connected to the box, and the median is in the middle of the box.

Personally think this is a relatively unintuitive graphic

import pandas as pd
import matplotlib.pyplot as plt
data=pd.read_excel('../data/catering_sale.xls',index_col='日期')
plt.rcParams['font.sans-serif']=['SimHei']#正常显示中文
plt.rcParams['axes.unicode_minus']=False#正常显示负号
plt.figure(figsize=(8,8))
p=data.boxplot(return_type='dict')
x=p['fliers'][0].get_xdata()
y=p['fliers'][0].get_ydata()
y.sort()
for i in range(len(x)):
  if i>0:
    plt.annotate(y[i],xy=(x[i],y[i]),xytext=(x[i]+0.05-0.8/(y[i]-y[i-1]),y[i]))
  else:
    plt.annotate(y[i],xy=(x[i],y[i]),xytext=(x[i]+0.8,y[i]))
plt.show()

Insert picture description here

Radar chart

The radar chart is a graphical method of displaying multivariate data in the form of a two-dimensional chart of three or more quantitative variables on an axis starting from the same point. The relative position and angle of the shaft are usually uninformed. Radar chart is also called network chart, spider chart, star chart, star chart, spider web chart, irregular polygon, polar coordinate chart or Kiviat chart. It is equivalent to a parallel coordinate diagram, with the axes arranged radially.

这是种我认为神奇的图形,可以综合评定一个各个方面的指标
The code below is reproduced in Add link description

from pyecharts import Radar
import os
os.getwcd()
schema = [('喜欢我', 100), ('听我话', 100), ('在乎我', 100), ('牵挂我', 100), ('主动找我', 100), ('逗我笑', 100), ('欺负我', 100),
          ('带我吃', 100), ('带我玩', 100), ('让着我', 100)]  # 写指标和最大值
v1 = [[99, 99, 99, 99, 99, 100, 10, 99, 99, 99]]  # 写对应指标的实际达到的数值
radar = Radar()
radar.config(schema)
radar.add('和xxx恋爱', v1, is_splitline=True, is_axisline_show=True)  # 标题
radar.show_config()
radar.render()

Insert picture description here
Okay, that's all for today's introduction, bye bye!

Guess you like

Origin blog.csdn.net/qq_41606378/article/details/88782349