今天畫的是些簡單的圖缴川,簡單線形圖茉稠、雷達(dá)圖和詞云圖。
同樣地把夸,先調(diào)包而线,設(shè)置整體字體和圖形樣式:
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import matplotlib.transforms as mtransforms
from matplotlib.ticker import FuncFormatter
from adjustText import adjust_text
from matplotlib import cm
#設(shè)置字體、圖形樣式
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['font.family']='sans-serif'
matplotlib.rcParams['axes.unicode_minus'] = False
matplotlib.rcParams['axes.spines.top'] = False
matplotlib.rcParams['axes.spines.right'] = False
1.簡單線形圖(simple_plot)
def simple_plot(xlabel,value,linestyle,filename):
fig=plt.figure(figsize=(7,4))
ax=fig.add_subplot()
ax.plot(xlabel,value,linestyle=linestyle)
ax.set_xticklabels(xlabel,rotation=20)
# 去掉邊框
orientation=['top','left','right']
for o in orientation:
ax.spines[o].set_visible(False)
# 去掉xticks
ax.set_yticks(())
# 添加文本
texts = [plt.text(xlabel[i], value[i], format(value[i],'.2f')) for i in range(len(xlabel))]
adjust_text(texts)
plt.tight_layout()
plt.savefig(filename,dpi=600)
plt.show()
# 每行n字設(shè)置
def add_newline(str,num):
n=len(str)//4
newstr=''
if len(str)%4==0:
for i in range(n):
newstr=newstr+str[i*num:(i+1)*num]+'\n'
else:
for i in range(n+1):
newstr=newstr+str[i*num:(i+1)*num]+'\n'
return newstr
def simple_plot_ex1():
xlabel=["校園教學(xué)硬件設(shè)施","課程選修制度","成績評定制度","評獎評優(yōu)制度","學(xué)生參與學(xué)校相關(guān)教育教學(xué)管理事務(wù)","心理咨詢服務(wù)",
"職業(yè)規(guī)劃與就業(yè)指導(dǎo)","醫(yī)學(xué)倫理與職業(yè)素養(yǎng)教育",
"社團(tuán)和文體活動","對所在專業(yè)教育教學(xué)質(zhì)量的總體評價(jià)","對所在大學(xué)教育教學(xué)質(zhì)量的總體評價(jià)"]
xlabel=[add_newline(x,4) for x in xlabel]
value=[3.720057,3.594829,3.75914,3.675722,3.538275,
3.587154,3.511715,3.818522,3.719047,3.904868,3.919511]
simple_plot(xlabel,value,linestyle='--',filename='simple_plot_ex1.png')
# 調(diào)用
simple_plot_ex1()
同樣是用plot
扎即,進(jìn)行相應(yīng)的設(shè)置,就可以畫出如下效果圖:
def float_plot(label,value,filename):
marker_radius=3.3
markeredgewidth=2
fig=plt.figure()
ax=fig.add_subplot()
plot=ax.plot(label,value,
linewidth=0,
marker='o',
markeredgewidth=markeredgewidth,
markersize=np.pi*marker_radius**2,
markerfacecolor='w'
)
for x,y in zip(label,value):
ax.text(x,y,format(y,'.2f'),ha='center',va='center')
ax.set_ylim([3,4])
xpos=ax.get_xticks()
ypos=ax.get_yticks()
for i in range(len(value)):
line=lines.Line2D([xpos[i],xpos[i]],[ax.set_ylim()[0],value[i]-(marker_radius+markeredgewidth)/72],
linestyle='-.',linewidth=1)
ax.add_line(line)
plt.tight_layout()
plt.savefig(filename,dpi=600)
def float_plot_ex():
label=['過程性參與','主動性參與','規(guī)則性參與']
value=[3.22,3.50,3.89]
float_plot(label,value,'float_plot.png')
# 調(diào)用
float_plot_ex()
2.簡單雷達(dá)圖(simple_radar)
def simple_radar(label,valuels,tag,colorname,filename):
label.append(label[0])
value=[]
for v in valuels:
firstvalue=v[0]
v.append(firstvalue)
value.append(v)
angles=np.linspace(0, 2*np.pi, len(label)-1, endpoint=False)
angles=np.concatenate((angles, [angles[0]]))
colorname=cm.get_cmap(name=colorname)
fillalpha=list(np.linspace(0.2, 1, len(value), endpoint=False))
fillalpha=fillalpha[::-1]
print(fillalpha)
fig=plt.figure()
ax=fig.add_subplot(111,polar=True)
for i in range(len(value)):
fillcolorls=colorname(i)
ax.plot(angles,value[i],'--',color=fillcolorls,label=tag[i],linewidth=1)
ax.fill(angles,value[i],facecolor=fillcolorls,alpha=fillalpha[i])
ax.set_thetagrids(angles * 180/np.pi, label)
ax.set_theta_zero_location('N')#設(shè)置極坐標(biāo)的起點(diǎn)(即0度)在正上方向
ax.set_ylim(min(min(value))-0.1,max(max(value))+0.1)
ax.legend(ncol=len(value), bbox_to_anchor=(0.5, -0.1),edgecolor='w',
loc='lower center', fontsize='small')
plt.savefig(filename,dpi=600)
plt.tight_layout()
plt.show()
def simple_radar_ex1():
label=["集體主義", "理想尊嚴(yán)至上","社會公正與正義","遵循社會道德","恪守學(xué)術(shù)道德"]
valuels=[[1.94,2.22,2.37,2.59,2.66],[1.8,2.29,2.44,2.62,2.65],
[2.02,2.3,2.4,2.57,2.55],[2.03,2.27,2.38,2.57,2.55]]
tag=['直轄市','東部','中部','西部']
simple_radar(label,valuels,colorname='Pastel1',tag=tag,filename='simple_radar1.png')
# 調(diào)用
simple_radar_ex1()
3.詞云圖(wordcloud)
def wc(backpic,txt,picname):
backpic=imageio.imread(backpic)
w=wordcloud.WordCloud(scale=0.8,background_color='white',
# max_words=300,
mask=backpic,
max_font_size=80,
font_path='msyh.ttc',
random_state=30)
w.generate(' '.join(jieba.lcut(txt)))
w.to_file(picname)
return picname
def wc_ex():
txt='會議強(qiáng)調(diào)况凉,要在做好常態(tài)化疫情防控的前提下谚鄙,繼續(xù)圍繞重點(diǎn)產(chǎn)業(yè)鏈、龍頭企業(yè)刁绒、重大投資項(xiàng)目闷营,打通堵點(diǎn)、連接斷點(diǎn),加強(qiáng)要素保障傻盟,促進(jìn)上下游速蕊、產(chǎn)供銷、大中小企業(yè)協(xié)同復(fù)工達(dá)產(chǎn)娘赴。要加快推動各類商場规哲、市場和生活服務(wù)業(yè)恢復(fù)到正常水平,暢通產(chǎn)業(yè)循環(huán)诽表、市場循環(huán)唉锌、經(jīng)濟(jì)社會循環(huán)。要加強(qiáng)國際協(xié)調(diào)合作竿奏,共同維護(hù)國際產(chǎn)業(yè)鏈供應(yīng)鏈安全穩(wěn)定袄简。'
wc('china_map(1).png',txt,'wordcloud.png')
# 調(diào)用
wc_ex()