Based matplotlib seaborn and data mining, data visualization

Using the Data visualization tools matplotlib python and seaborn visualize
the required data using Part numpy libraries and library generation pandas array, matrix, dataframe.

  • Import the required libraries:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

  • FIG drawing functions (plt.plot ())
Select the graphic theme plt.sytle.use (): five kinds of themes: Dark Grid (darkgrid), white mesh (whitegrid), black (dark), all white (white), full scale (ticks) defaults to full scale
Plotted in FIG. matplotlib.pyplot.plot (* args, scalex = True, scaley = True, data = None, ** kwargs): * args include the required data, color and style of curve
Display graphics matplotlib.pyplot.show(args,* kw )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

plt.style.use("seaborn-darkgrid")
#定义x
x=np.arange(0,3*np.pi,0.1)
#生成正弦函数
y=np.sin(x)
#调用plot函数实现可视化
plt.plot(x,y)
plt.show()

Here Insert Picture Description

  • Draw subgraph:
    the painting into a Bucher a subgraph
    above, relating to generation function
Add subgraph matplotlib.pyplot.subplot (* args, ** kwargs): adding the current submap in FIG. * Args is an integer of 3 or an integer of three separate, the location descriptor of FIG.
Drawing subgraph plt.plot ()
Sub-graph display plt.show()
Set Title plt.title(" ")
plt.style.use("seaborn-darkgrid")
x=np.arange(0,4*np.pi,0.1)
y=np.sin(x)
y2=np.cos(x)
#将原图分成两行一列两个子图,并取第一个子图
plt.subplot(2,1,1)
plt.plot(x,y)
plt.subplot(2,1,2)
plt.plot(x,y2)
plt.show()

Here Insert Picture Description

  • Modify the parameter value
Set the image size plt.figure (figsize = (8,6), dpi = 80) # Create a 8x6 sized image, dpi = 80 indicates 80 points per inch resolution
Curve set color, width, label style plt.plot (X, C, color = "blue", linewidth = 1.0, label = "Blue", linestyle = "-") C denotes a function of blue, a line width of one pixel, sign legend table " Blue ", linestyle a graph style
Display settings legend plt.legend ()
Range provided the x-axis plt.xlim ()
Setting the x axis scale plt.xticks ()
Set y-axis range plt.ylim ()
Set y axis scale plt.yticks ()
Save image plt.savefig()
plt.style.use("seaborn-dark")
plt.figure(figsize=(4,3),dpi=80)
#创建一个1*!的子图 取第一个
plt.subplot(111)
x=np.linspace(-np.pi, np.pi, 256,endpoint=True)
C=np.cos(x)
S=np.sin(x)
plt.plot(x, C, color="blue",linewidth=1.0,label="Blue",linestyle="--")
# 绘制一个绿色的,线宽为1个像素的正弦曲线,设置图例标签Blue,linestyle表示曲线的样式
plt.plot(x, S, color="green", linewidth=1.0, label="Green", linestyle="-.")

plt.legend()
plt.xlim(-4.0,4.0)
plt.xticks(np.linspace(-4,4,9,endpoint=True))
plt.ylim(-1.0,1.0)
plt.yticks(np.linspace(-1,1,5,endpoint=True))
plt.show()

Here Insert Picture Description

  • Draw Scatter
    matplotlib achieve Scatter
Draw a scatter plot matplotlib.pyplot.scatter (x, y, s = None, c = None, marker = None, cmap = None, norm = None, vmin = None, vmax = None, alpha = None, linewidths = None, verts = None, edgecolors = None, *, data = None, ** kwargs): draw scattergram. X, y-axis data representing xy, s represents a scalar, c denotes color, marker tag indicates style.
a = np.random.randint(0,20,15)	# 随机生成数据
b = np.random.randint(0,20,15)
print(a)
print(b)i
plt.scatter(a, b)					# 绘制散点图
plt.show()

Here Insert Picture Description
seaborn achieve Scatter:
create dataframe

Draw a scatter plot seaborn.jointplot (x, y, data = None, kind = 'scatter', stat_func = None, color = None, height = 6, ratio = 5, space = 0.2, dropna = True, xlim = None, ylim = None, joint_kws = None, marginal_kws = None, annot_kws = None, ** kwargs): with two variables are plotted. color: color; size: 6 default, the scale size of the map (squares); ratio: the ratio of the center of FIG. FIG side edge; space: FIG interval size and the center side map; s: the dot size; linewidth: line width; {x, y} lim: x, y-axis range.
data=pd.DataFrame(np.random.randn(5,2),columns=list('AB'))
print(data)
sns.jointplot(x='A',y='B',data=data)
plt.show()

Here Insert Picture Description
Here Insert Picture Description

  • A histogram
matplotlib achieve Histogram matplotlib.pyplot.bar (x, height, width = 0.8, bottom = None, *, align = 'center', data = None, ** kwargs): a histogram. X: abscissa; height: height of the bar; width: each strip width, color: the color of each bar.
Add a grid display plt.grid ()
seaborn achieve Histogram seaborn.countplot (x = None, y = None, hue = None, data = None, order = None, hue_order = None, orient = None, color = None, palette = None, saturation = 0.75, dodge = True, ax = none, ** kwargs): histogram. x, y: xy axis; data: data; hue: forming a histogram classified according to the classification in the column name value; order, hue_order: histogram for controlling the sequence; palette: palette, controls a different color.
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']	# 解决中文不显示问题
level = ['tk', 'shtk', 'hztk']
x = range(len(level))		# 横坐标
y = [1,3,2]				# 纵坐标
plt.figure(figsize=(4,3),dpi=80)		# 创建画布
plt.bar(x, y, width=0.5, color=['b','r','g'])# 绘制柱状图
plt.xticks(x,level)
plt.grid(linestyle="--", alpha=0.5) # 添加网格显示
plt.show()

Here Insert Picture Description

df = pd.DataFrame(['优秀',"不错","666","666","不错","不错"],columns=["level"])
sns.countplot(x="level",data=df)
plt.figure(figsize=(4,3),dpi=80)
plt.show()

Here Insert Picture Description

  • Draw a histogram
matplotlib histogram for matplotlib.pyplot.hist (x, bins = None, range = None, density = None, weights = None, cumulative = False, bottom = None, histtype = 'bar', align = 'mid', orientation = 'vertical', rwidth = None, log = False, color = None, label = None, stacked = False, normed = None, *, data = None, ** kwargs): histogrammed. X: specify each bin (bin) of data distribution, corresponding to the x-axis; bins: Specifies the number of bin (box), i.e. a total of several bar chart; normed: specified density, that is, each bar graph the proportion ratio, the default is 1; color: specifies the color of the bar graph.
seaborn histogram for seaborn.distplot (a, bins = None, hist = True, kde = True, rug = False, fit = None, hist_kws = None, kde_kws = None, rug_kws = None, fit_kws = None, color = None, vertical = False, norm_hist = False, axlabel = None, label = None, ax = None): histogrammed. A: Data; hist: whether to display the histogram; kde: whether kernel density estimation; bins: dividing the control histogram; fit: fitting parameter map control
x = np.random.randint(0,30,90)
print(x)
plt.figure(dpi=100)
distance = 2		# 设置组距
plt.hist(x,facecolor="blue", edgecolor="black", alpha=0.7) # 绘制直方图
plt.xticks(range(min(x), max(x))[::2]) # 修改x轴刻度显示
plt.grid(linestyle="--", alpha=0.5) # 添加网格显示
plt.show()#  显示图像


Here Insert Picture Description

sns.distplot(x, kde=True)

Here Insert Picture Description

Published 20 original articles · won praise 23 · views 987

Guess you like

Origin blog.csdn.net/surijing/article/details/104649327