【前言】: 1、在上一篇文章中,我们讲述了如果画一些简单的图表,比如:柱状图、条形图、饼图等等。 2、这篇文章就来一些干货,总结一下关于画布的相关知识,以及如何创建多个子图。
fihure() 函数
生成一个画布--代码实现
import matplotlib.pyplot as plt
import pandas as pd
Data_fram=pd.read_excel('E:\pythonProject\matplotlib各种图表\line_chart_bar.xlsx')
print(Data_fram.head())
plt.rcParams['font.sans-serif'] = ['KaiTi']
plt.rcParams['axes.unicode_minus'] = False
canvas=plt.figure()
map1=canvas.add_subplot(2,2,1)
map2=canvas.add_subplot(222)
map3=canvas.add_subplot(223)
plt.plot(Data_fram['水果'],Data_fram['销售百分比'],color='red',marker='*',ms=10)
map4=canvas.add_subplot(224)
plt.bar(Data_fram['水果'],Data_fram['销售量'],color='orange')
plt.show()
效果展示
subplot创建单个子图
生成单个子图--代码实现
import matplotlib.pyplot as plt
import pandas as pd
Data_fram=pd.read_excel('E:\pythonProject\matplotlib各种图表\line_chart_bar.xlsx')
print(Data_fram.head())
plt.rcParams['font.sans-serif'] = ['KaiTi']
plt.rcParams['axes.unicode_minus'] = False
canvas=plt.figure()
plt.subplot(221)
plt.subplot(222)
plt.plot(Data_fram['水果'],Data_fram['销售百分比'],color='red',marker='*',ms=10)
plt.subplot(223)
plt.bar(Data_fram['水果'],Data_fram['销售量'],color='blue')
plt.show()
效果展示
subplots创建多个子图
与上面的subplots就多了一个s 生成单多个子图--代码实现
import matplotlib.pyplot as plt
import pandas as pd
Data_fram=pd.read_excel('E:\pythonProject\matplotlib各种图表\line_chart_bar.xlsx')
print(Data_fram.head())
plt.rcParams['font.sans-serif'] = ['KaiTi']
plt.rcParams['axes.unicode_minus'] = False
canvas,map=plt.subplots(2,2)
map1=map[0,0]
map2=map[0,1]
map3=map[1,0]
map4=map[1,1]
map3.plot(Data_fram['水果'],Data_fram['销售百分比'],color='red',marker='*',ms=10)
map4.bar(Data_fram['水果'],Data_fram['销售量'],color='blue')
plt.show()
效果展示
add_subplots与add_axes新增子图或区域
add_subplots
add_subplots--代码实现
import matplotlib.pyplot as plt
import pandas as pd
Data_fram=pd.read_excel('E:\pythonProject\matplotlib各种图表\line_chart_bar.xlsx')
print(Data_fram.head())
plt.rcParams['font.sans-serif'] = ['KaiTi']
plt.rcParams['axes.unicode_minus'] = False
canvas=plt.figure()
map1=canvas.add_subplot(221)
map1.plot(Data_fram['水果'],Data_fram['销售百分比'],color='red',marker='*',ms=10)
map4=canvas.add_subplot(224)
map4.bar(Data_fram['水果'],Data_fram['销售量'],color='blue')
plt.show()
效果展示
add_axes
add_axes--代码实现
import matplotlib.pyplot as plt
import pandas as pd
Data_fram=pd.read_excel('E:\pythonProject\matplotlib各种图表\line_chart_bar.xlsx')
print(Data_fram.head())
plt.rcParams['font.sans-serif'] = ['KaiTi']
plt.rcParams['axes.unicode_minus'] = False
canvasss=plt.figure()
left,bottom,width,height = 0.1,0.1,0.8,0.8
mapp1=canvasss.add_axes([left,bottom,width,height ])
mapp1.plot(Data_fram['水果'],Data_fram['销售百分比'],color='red',marker='*',ms=10)
mapp1.set_title("销售量")
left,bottom,width,height = 0.65,0.6,0.25,0.25
mapp2=canvasss.add_axes([left,bottom,width,height ])
mapp2.bar(Data_fram['水果'],Data_fram['销售量'],color='blue')
mapp2.set_title("销售百分比")
plt.show()
效果展示
|