本文基于python介绍五种图的画法,分别是:散点图、柱状图、折线图、层叠柱状图、相关热力图
以下所用到的数据均出自2021年长三角数学建模比赛,想要实验的可从下面的百度网盘链接获取
链接:https://pan.baidu.com/s/1Jl7ljHuJvGbusDUS2ODd6w?pwd=g4rv
提取码:g4rv
目录
1、散点图:
2、柱状图
3、折线图_简略
4、动态命名技术
5、多图技术
6、层叠柱状图
7、相关热力图
plt.scatter(ind, ydata, width,color = 'r')
用例:
import pandas as pd
from pandas import DataFrame
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
infodata = pd.read_csv('data.csv',low_memory = False)
types = infodata['类型'].tolist()
school_level = infodata['办学层次'].tolist()
subject_level = infodata['学科水平'].tolist()
resources = infodata['办学资源'].tolist()
typelst = list(set(types))
level = []
for i in range(0,len(typelst)):
Sum = 0
num = 0
for j in range(0,len(types)):
if typelst[i]==types[j]:
Sum = Sum + school_level[j]
num = num + 1
level.append(Sum/num)
matplotlib.rc("font", family='Microsoft YaHei') #中文
xdata = typelst
ydata = level
ind = np.arange(len(level))
width = 10
plt.ylabel('办学层次')
plt.title('不同类型学校办学层次散点图')
plt.scatter(ind, ydata, width,color = 'r')
plt.xticks(ind, xdata)
plt.show()
plt.bar(ind, ydata, width,color = 'RoyalBlue')
用例:
import pandas as pd
from pandas import DataFrame
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
infodata = pd.read_csv('data.csv',low_memory = False)
types = infodata['类型'].tolist()
school_level = infodata['办学层次'].tolist()
subject_level = infodata['学科水平'].tolist()
resources = infodata['办学资源'].tolist()
typelst = list(set(types))
level = []
for i in range(0,len(typelst)):
Sum = 0
num = 0
for j in range(0,len(types)):
if typelst[i]==types[j]:
Sum = Sum + subject_level[j]
num = num + 1
level.append(Sum/num)
matplotlib.rc("font", family='Microsoft YaHei') #中文
xdata = typelst
ydata = level
ind = np.arange(len(level))
width = 0.35
plt.ylabel('学科水平')
plt.title('不同类型学校学科水平柱状图')
plt.bar(ind, ydata, width,color = 'RoyalBlue')
plt.xticks(ind, xdata)
plt.show()
plt.plot(xdata, ydata, 'b-', alpha=0.5, linewidth=1)
plt.title(title[k])
plt.legend()
plt.xticks(ind, typelst)
plt.show()
figure_name = '药品' + str(i + 1) + 'LineChart.html'
trace = [go.Scatter (x = date_time, y = soldMonthPrice[i], mode = 'lines+markers', name = 'lines+markers')]
layout = dict(title = '2020-2021月销售总额')
fig = dict(data = trace, layout = layout)
pyplt (fig, filename=figure_name)
用例演示_柱状图_折线图
import pandas as pd
from pandas import DataFrame
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import plotly.graph_objs as go
infodata = pd.read_csv('data.csv',low_memory = False)
types = infodata['类型'].tolist()
school_level = infodata['办学层次'].tolist()
subject_level = infodata['学科水平'].tolist()
resources = infodata['办学资源'].tolist()
title = ['办学层次','学科水平','办学资源','师资规模与结构','人才培养','科学研究','服务社会','高端人才','重大项目与成果','国际竞争力']
color = ['r-','b-','y-','g-','c-']
solddata = [] #二维数组
matplotlib.rc("font", family='Microsoft YaHei') #中文
for k in range(0,10):
data = infodata[title[k]].tolist()
typelst = list(set(types))
level = []
for i in range(0,len(typelst)):
Sum = 0
num = 0
for j in range(0,len(types)):
if typelst[i]==types[j]:
Sum = Sum + data[j]
num = num + 1
level.append(Sum/num)
solddata.append(level)
figure_name = title[k] + 'lineChart.html'
trace = [go.Scatter (x = typelst, y = solddata[k], mode = 'lines+markers', name = 'lines+markers')]
ind = np.arange(len(typelst))
#折线图
# plt.plot(typelst, solddata[k], color[k % 5], alpha=0.5, linewidth=1, label = title[k])
width = 0.35
p = plt.bar(typelst, solddata[k], width)
plt.title(title[k])
plt.bar_label(p, label_type='edge')
plt.legend()
plt.xticks(ind, typelst)
plt.show()
灵活运用 plt.show 即可
import pandas as pd
from pandas import DataFrame
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import plotly.graph_objs as go
infodata = pd.read_csv('data.csv',low_memory = False)
types = infodata['类型'].tolist()
school_level = infodata['办学层次'].tolist()
subject_level = infodata['学科水平'].tolist()
resources = infodata['办学资源'].tolist()
title = ['办学层次','学科水平','办学资源','师资规模与结构','人才培养','科学研究','服务社会','高端人才','重大项目与成果','国际竞争力']
color = ['r-','b-','y-','g-','c-']
solddata = [] #二维数组
matplotlib.rc("font", family='Microsoft YaHei') #中文
for k in range(0,10):
data = infodata[title[k]].tolist()
typelst = list(set(types))
level = []
for i in range(0,len(typelst)):
Sum = 0
num = 0
for j in range(0,len(types)):
if typelst[i]==types[j]:
Sum = Sum + data[j]
num = num + 1
level.append(Sum/num)
solddata.append(level)
figure_name = title[k] + 'lineChart.html'
trace = [go.Scatter (x = typelst, y = solddata[k], mode = 'lines+markers', name = 'lines+markers')]
ind = np.arange(len(typelst))
plt.plot(typelst, solddata[k], color[k % 5], alpha=0.5, linewidth=1, label = title[k])
plt.legend()
plt.xticks(ind, typelst)
if k%5 == 4:
plt.show()
plt.rcParams['figure.figsize']=10,6
ind = np.arange(5)
width = 0.35
ydata1 = solddata[0]
ydata2 = solddata[1]
ydata3 = solddata[2]
ydata4 = solddata[3]
ydata5 = solddata[4]
xdata = typelst
p1 = plt.bar(ind, ydata1, width, color='LightSkyBlue')
p2 = plt.bar(ind, ydata2, width, color='RoyalBlue', bottom=ydata1)
p3 = plt.bar(ind, ydata3, width, color='MediumPurple', bottom=np.sum([ydata1, ydata2], axis=0).tolist())
p4 = plt.bar(ind, ydata4, width, color='Lavender', bottom=np.sum([ydata1, ydata2, ydata3], axis=0).tolist())
p5 = plt.bar(ind, ydata5, width, color='LightPink', bottom=np.sum([ydata1, ydata2, ydata3, ydata4], axis=0).tolist())
plt.ylabel('累计总量')
plt.title('办学层次、学科水平、办学资源、师资规模与结构和人才培养层叠柱状图')
plt.xticks(ind, xdata)
plt.legend((p1[0], p2[0], p3[0], p4[0], p5[0] ), (title[0],title[1],title[2],title[3],title[4]))
plt.show()
用例:
import pandas as pd
from pandas import DataFrame
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import plotly.graph_objs as go
infodata = pd.read_csv('data.csv',low_memory = False)
types = infodata['类型'].tolist()
school_level = infodata['办学层次'].tolist()
subject_level = infodata['学科水平'].tolist()
resources = infodata['办学资源'].tolist()
title = ['办学层次','学科水平','办学资源','师资规模与结构','人才培养','科学研究','服务社会','高端人才','重大项目与成果','国际竞争力']
color = ['r-','b-','y-','g-','c-']
solddata = [] #二维数组
matplotlib.rc("font", family='Microsoft YaHei') #中文
for k in range(0,10):
data = infodata[title[k]].tolist()
typelst = list(set(types))
level = []
for i in range(0,len(typelst)):
Sum = 0
num = 0
for j in range(0,len(types)):
if typelst[i]==types[j]:
Sum = Sum + data[j]
num = num + 1
level.append(Sum/num)
solddata.append(level)
plt.rcParams['figure.figsize']=10,6
ind = np.arange(5)
width = 0.35
ydata1 = solddata[0]
ydata2 = solddata[1]
ydata3 = solddata[2]
ydata4 = solddata[3]
ydata5 = solddata[4]
xdata = typelst
p1 = plt.bar(ind, ydata1, width, color='LightSkyBlue')
p2 = plt.bar(ind, ydata2, width, color='RoyalBlue', bottom=ydata1)
p3 = plt.bar(ind, ydata3, width, color='MediumPurple', bottom=np.sum([ydata1, ydata2], axis=0).tolist())
p4 = plt.bar(ind, ydata4, width, color='Lavender', bottom=np.sum([ydata1, ydata2, ydata3], axis=0).tolist())
p5 = plt.bar(ind, ydata5, width, color='LightPink', bottom=np.sum([ydata1, ydata2, ydata3, ydata4], axis=0).tolist())
plt.ylabel('累计总量')
plt.title('办学层次、学科水平、办学资源、师资规模与结构和人才培养层叠柱状图')
plt.xticks(ind, xdata)
plt.legend((p1[0], p2[0], p3[0], p4[0], p5[0] ), (title[0],title[1],title[2],title[3],title[4]))
plt.show()
data = {'办学层次':solddata[0],'学科水平':solddata[1],'办学资源':solddata[2],'师资规模与结构':solddata[3],'人才培养':solddata[4],'科学研究':solddata[5],'服务社会':solddata[6],'高端人才':solddata[7],'重大项目与成果':solddata[8],'国际竞争力':solddata[9],'综合评分':solddata[10]}
df = DataFrame(data)
df_corr = df.corr()
plt.figure(figsize=(20, 14))
plt.title('相关性热力图')
fig = sns.heatmap(df_corr, annot=True, fmt='.2g', cmap="YlGnBu")
plt.show()
plt.savefig("heatmap.png")
用例:
import pandas as pd
from pandas import DataFrame
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import plotly.graph_objs as go
import seaborn as sns
infodata = pd.read_csv('data.csv',low_memory = False)
types = infodata['类型'].tolist()
school_level = infodata['办学层次'].tolist()
subject_level = infodata['学科水平'].tolist()
resources = infodata['办学资源'].tolist()
title = ['办学层次','学科水平','办学资源','师资规模与结构','人才培养','科学研究','服务社会','高端人才','重大项目与成果','国际竞争力','综合评分']
color = ['r-','b-','y-','g-','c-']
solddata = [] #二维数组
matplotlib.rc("font", family='Microsoft YaHei') #中文
for k in range(0,11):
data = infodata[title[k]].tolist()
typelst = list(set(types))
level = []
for i in range(0,len(typelst)):
Sum = 0
num = 0
for j in range(0,len(types)):
if typelst[i]==types[j]:
Sum = Sum + data[j]
num = num + 1
level.append(Sum/num)
solddata.append(level)
data = {'办学层次':solddata[0],'学科水平':solddata[1],'办学资源':solddata[2],'师资规模与结构':solddata[3],'人才培养':solddata[4],'科学研究':solddata[5],'服务社会':solddata[6],'高端人才':solddata[7],'重大项目与成果':solddata[8],'国际竞争力':solddata[9],'综合评分':solddata[10]}
df = DataFrame(data)
df_corr = df.corr()
plt.figure(figsize=(20, 14))
plt.title('相关性热力图')
fig = sns.heatmap(df_corr, annot=True, fmt='.2g', cmap="YlGnBu")
plt.show()
plt.savefig("heatmap.png")