matplotlib
1. Line graph: plt.plot (parameter)
X轴,Y轴坐标的数据格式可以是列表、数组和Series。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
# 创建数据
data = {
'name':['张三', '李四', '王五', '小明'],
'sex':['female', 'female', 'male', 'male'],
'math':[78, 79, 83, 92],
'city':['北京', '上海', '广州', '北京']
}
df = pd.DataFrame(data)
df
# X轴为行索引,Y轴为Series数据
plt.plot(df.index,df['math'])
- The color parameter specifies the color of the line
years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]
gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3]
plt.plot(years,gdp,color='r')
# 也可以通过RGB指定
plt.plot(years,gdp,color='#FFA500')
- The linestyle parameter specifies the shape of the line
plt.plot(years,gdp,linestyle='--')
- The linewidth parameter specifies the width of the line
plt.plot(years,gdp,linestyle='--',linewidth=5)
- The marker parameter can mark the coordinate points
plt.plot(years,gdp,marker='.') # 小圆点
plt.plot(years,gdp,marker='D') # 方点
- Color, line and point styles can be placed together in the format string, but the color setting should be placed in front of the line and point style
plt.plot(years,gdp,'ro--')
2. Histogram
-(1) Basic histogram
-
bar function: plot.bar (parameter)
- color - 柱状图的填充色 - alpha - 透明度
# 需要传入刻度列表和高度列表
data = [23, 85, 72, 43, 52]
plt.bar([1,2,3,4,5],data)
# 设置颜色和透明度
plt.bar(range(len(data)),data,color='royalblue',alpha=0.7)
-
Grid function to draw grid: plt.grid (parameter)
- color - 颜色 - linestyle - 线条形状 - linewidth - 线条宽度 - axis - 指定轴 - alpha - 透明度
plt.bar(range(len(data)),data,color='green',alpha=0.7)
# 绘制格网
plt.grid(color='#95a5a6',linestyle='--', linewidth=1,axis='y',alpha=0.6)
-(2) Stacked histogram
- bottom参数
- 设置柱状图的高度,即哪个数据作为柱状图底部
data1 = [23, 85, 72, 43, 52]
data2 = [42, 35, 21, 16, 9]
plt.bar(range(len(data)),data1)
plt.bar(range(len(data)),data2,bottom=data1) # data1垫底
-(3) Side by side histogram
- width参数
- 设置柱状图的宽度
data1 = [23, 85, 72, 43, 52]
data2 = [42, 35, 21, 16, 9]
width = 0.3 # 设置柱状图宽度为0.3
plt.bar(np.arange(len(data1)),data1,width=width)
plt.bar(np.arange(len(data2))+width,data2,width=width)
-(4) Horizontal histogram
- barh函数
data1 = [23, 85, 72, 43, 52]
plt.barh(range(len(data1)),data1)
3. Other basic charts
-(1) Scatter chart
- scatter函数
# 案列1
X = np.random.randn(100)
Y = np.random.randn(100)
plt.scatter(X,Y)
# 设置颜色、
plt.scatter(X,Y,color='red',marker='D')
# 案例2
"""python绘制散点图"""
from matplotlib import pyplot as plt
import numpy as np
train_x = np.linspace(-1, 1, 100)
train_y_1 = 2*train_x + np.random.rand(*train_x.shape)*0.3
train_y_2 = train_x**2+np.random.randn(*train_x.shape)*0.3
plt.scatter(train_x, train_y_1, c='red', marker='v' )
plt.scatter(train_x, train_y_2, c='blue', marker='o' )
plt.legend(["red","Blue"], loc="upper lift")
plt.show()
-(2) Histogram
- hist函数
x = np.random.normal(size=100)
plt.hist(x,bins=30)
4. Custom settings
- (1)add_subplot()
- fig=plt.figure(figsize=(10,6))
fig = plt.figure(figsize=(10,6)) # 设置图表的长宽比
ax1 = fig.add_subplot(2,2,1) # 创建子图
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]
gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3]
ax1.scatter(years,gdp)
ax2.plot(years,gdp)
ax3.bar(years,gdp)
- (2)plt.subplots()
# 创建子图,返回图对象和子图坐标
years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]
gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3]
fig,axes = plt.subplots(2,2,figsize=(10,6))
axes[0,1].plot(years,gdp)
Supplement: scale and label, legend
Scale and label:
data = [23, 85, 72, 43, 52]
labels = ['A','B','C','D','E']
plt.xticks(range(len(data)),labels) #设置刻度和标签
plt.bar(range(len(data)),data)
data = [23, 85, 72, 43, 52]
labels = ['A','B','C','D','E']
plt.xticks(range(len(data)),labels) #设置刻度和标签
plt.xlabel('Class')
plt.ylabel('Amounts')
plt.title('Example1')
plt.bar(range(len(data)),data)
legend:
data1 = [23, 85, 72, 43, 52]
data2 = [42, 35, 21, 16, 9]
width = 0.3
plt.bar(np.arange(len(data1)),data1,width=width,label='one')
plt.bar(np.arange(len(data2))+width,data2,width=width,label='two') # 设置图例名称,one、two
plt.legend() # 绘制图例