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:
[]
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)
In:
#color
rdat = np.random.rand(10)
plt.plot(rdat,color='b') #r红色,g绿色
out:
[]
In:
#marker
plt.plot(rdat,marker = 'x')
out:
[]
In:
#linestyle
plt.plot(rdat,linestyle='-.') #: -- -.
out:
[]
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
In:
fig2 = plt.figure(figsize=(8,4))
ax1 = fig2.add_subplot(121)
ax2 = fig2.add_subplot(122)
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;
}
In:
#折线图:单一连续性指标
x = np.arange(1,11)
y = np.arange(10,101,10)
plt.plot(x,y)
out:
[]
In:
y2 = np.arange(20,201,20)
fig2 = plt.figure()
ax = fig2.add_subplot(111)
ax.plot(x,y)
ax.plot(x,y2)
out:
[]
In:
#散点图:两个连续性变量的关系
plt.scatter(tips['total_bill'],tips['tip'])
out:
In:
#柱状图:分类型变量指标呈现
x = np.arange(5)
y = np.random.rand(5)
plt.bar(x,y)
out:
In:
plt.barh(x,y)
out:
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]),
)
In:
#饼图:分类型变量指标呈现
labels = list('abcd')
rates = [10,50,60,40]
plt.pie(rates,autopct='%.2f',labels=labels)
plt.legend(labels=labels)
out:
In:
#箱线图或盒图 boxplot,场景:单个连续性变量的统计分析
plt.boxplot(tips['tip'])
out:
{'whiskers': [,
],
'caps': [,
],
'boxes': [],
'medians': [],
'fliers': [],
'means': []}