22.Python可视化-Matplotlib

Matplotlib实验

In:

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

out:

D:\ProgramData\Anaconda3\lib\importlib\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject
  return f(*args, **kwds)

In:

plt.plot(np.random.rand(10))

out:

[]

22.Python可视化-Matplotlib_第1张图片

In:

df1 = pd.DataFrame(np.random.rand(10,2),columns=['a','b'])

In:

fig2 = df1.plot(figsize=(6,4)) #通过pandas画布
plt.title("Demo Title")
plt.xlabel("x")
plt.ylabel("y")
plt.xlim([0,10])
plt.ylim([0,1])
plt.legend(loc = 'upper right')
print(fig2)

out:

AxesSubplot(0.125,0.125;0.775x0.755)

22.Python可视化-Matplotlib_第2张图片

图表样式

In:

#color
rdat = np.random.rand(10)
plt.plot(rdat,color='b') #r红色,g绿色

out:

[]

22.Python可视化-Matplotlib_第3张图片

In:

#marker
plt.plot(rdat,marker = 'x')

out:

[]

22.Python可视化-Matplotlib_第4张图片

In:

#linestyle
plt.plot(rdat,linestyle='-.') #: -- -.

out:

[]

22.Python可视化-Matplotlib_第5张图片

布局,子图

In:

fig = plt.figure() #matplotlib创建画布

In:

ax = fig.add_subplot(111)

out:

D:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
  """Entry point for launching an IPython kernel.

In:

ax.set(title="demo2",xlabel='x1')

out:

[Text(0.5, 3.1999999999999993, 'x1'), Text(0.5, 1, 'demo2')]

In:

fig

22.Python可视化-Matplotlib_第6张图片

In:

fig2 = plt.figure(figsize=(8,4))
ax1 = fig2.add_subplot(121)
ax2 = fig2.add_subplot(122)

22.Python可视化-Matplotlib_第7张图片

In:

tips = pd.read_table("../../Course/data/tips.csv",sep=',')
tips.head()

out:

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

22.Python可视化-Matplotlib_第8张图片

In:

#折线图:单一连续性指标
x = np.arange(1,11)
y = np.arange(10,101,10)
plt.plot(x,y)

out:

[]

22.Python可视化-Matplotlib_第9张图片

In:

y2 = np.arange(20,201,20)
fig2 = plt.figure()
ax = fig2.add_subplot(111)
ax.plot(x,y)
ax.plot(x,y2)

out:

[]

22.Python可视化-Matplotlib_第10张图片

In:

#散点图:两个连续性变量的关系
plt.scatter(tips['total_bill'],tips['tip'])

out:


22.Python可视化-Matplotlib_第11张图片

In:

#柱状图:分类型变量指标呈现
x = np.arange(5)
y = np.random.rand(5)
plt.bar(x,y)

out:


22.Python可视化-Matplotlib_第12张图片

In:

plt.barh(x,y)

out:


22.Python可视化-Matplotlib_第13张图片

In:

#直方图: 连续性变量的频次分布
x = np.random.rand(100,1)
bins= 10
plt.hist(x,bins=bins,rwidth=0.9)

out:

(array([12., 13., 10.,  6.,  9.,  7.,  7., 13.,  8., 15.]),
 array([0.0076997 , 0.10669429, 0.20568889, 0.30468348, 0.40367808,
        0.50267267, 0.60166726, 0.70066186, 0.79965645, 0.89865105,
        0.99764564]),
 )

22.Python可视化-Matplotlib_第14张图片

In:

#饼图:分类型变量指标呈现
labels = list('abcd')
rates = [10,50,60,40]
plt.pie(rates,autopct='%.2f',labels=labels)
plt.legend(labels=labels)

out:


22.Python可视化-Matplotlib_第15张图片

In:

#箱线图或盒图 boxplot,场景:单个连续性变量的统计分析
plt.boxplot(tips['tip'])

out:

{'whiskers': [,
  ],
 'caps': [,
  ],
 'boxes': [],
 'medians': [],
 'fliers': [],
 'means': []}

22.Python可视化-Matplotlib_第16张图片

你可能感兴趣的:(Python可视化,可视化,python,数据可视化)