龙空技术网

python-matplotlib(字体、文本、颜色、样式)

丘比特伤脑筋 365

前言:

当前姐妹们对“python输出带颜色的文字”大约比较关切,各位老铁们都想要剖析一些“python输出带颜色的文字”的相关文章。那么小编在网摘上汇集了一些关于“python输出带颜色的文字””的相关知识,希望你们能喜欢,我们一起来了解一下吧!

matplotlib.rc()

import matplotlib as mpl# method1 of setting attributionmpl.rc('lines',linewidth=2,color='c',linestyle='--')# method2 of setting attributionline = {'linewidth':2,'color':'c','linestyle':'--'}mpl.rc('lines',**line)

matplotlbi.rcParams

import matplotlib as mpl#usage of package matplotlibmpl.rcParams['lines.linewidth']=2mpl.rcParams['lines.color']='c'mpl.rcParams['lines.linestyle']='--'

在画图过程中设置

import matplotlib.pyplot as pltimport numpy as np# normal plotplt.axes([.1,.7,.3,.3], frameon=True, aspect='equal')plt.plot(np.arange(3),[0,1,0])plt.cla()plt.plot(np.arange(3),[0,1,0])# no-plotplt.axes([.4,.4,.3,.3], frameon=True, aspect='equal')plt.plot(2+np.arange(3),[0,1,0])# no-axesplt.axes([.7,.1,.3,.3], frameon=True, aspect='equal')plt.plot(4+np.arange(3),[0,1,0])plt.axis('off')plt.show()
# family 字体serifsans-serifcursivefantasymonospace
# style 字体样式normalitalicoblique
# weight 字体粗细a numeric value in range 0-1000ultralightligthnormalregularbookmediumromansemibolddemibolddemiboldheavyextra boldblack
# size 字体大小size in pointsxx-smallx-smallsamllmediumlargex-largexx-large
# variant 字体变体normalsmall-caps
import matplotlib.pyplot as pltfig = plt.figure()ax = fig.add_subplot(111)families = ['serif','sans-serif','fantasy','monospace']ax.text(-1,1,'family',fontsize=18,horizontalalignment='center')pi = [.9,.8,.7,.6,.5,.4,.3,.2,.1]for i,family in enumerate(families):    ax.text(-1,pi[i],family,family=family,horizontalalignment='center')sizes = ['xx-small','x-small','small','medium','large','x-large','xx-large']ax.text(-.5,1,'size',fontsize=18,horizontalalignment='center')for i,size in enumerate(sizes):    ax.text(-.5,pi[i],size,size=size,horizontalalignment='center')styles = ['normal','italic','oblique']ax.text(0,1,'style',fontsize=18,horizontalalignment='center')for i,style in enumerate(styles):    ax.text(0,pi[i],style,family='sans-serif',style=style,horizontalalignment='center')        variants = ['normal','small-caps']        ax.text(.5,1,'variant',fontsize=18,horizontalalignment='center')        weights = ['light','normal','semibold','bold','black']    for i,weight in enumerate(weights):    ax.text(1,pi[i],weight,weight=weight,horizontalalignment='center')        ax.axis([-1.5,1.5,.1,1.1])    ax.set_xticks([])    ax.set_yticks([])        plt.show()

颜色参数

barSlices = 12theta = np.linspace(0,2*np.pi,barSlices,endpoint=False)radii = 30*np.random.rand(barSlices)width = 2*np.pi/barSlicescolors = np.array(['c','m','y','b','#C67171','#C1CDCD','#FFEC8B','#A0522D','red','burlywood','chartreuse','green'])fig = plt.figure()ax = fig.add_subplot(111, polar=True)bars = ax.bar(theta,radii,width=width,color=colors,bottom=0)plt.show()

调色板

# method1matplotlib.pyplot.imshow(X,cmap=matplotlib.cm.hot)matplotlib.pyplot.scatter(X,Y,c=numpy.random.rand(10),cmap=matplotlib.cm.jet)# method2matplotlib.pyplot.imshow(X)matplotlib.pyplot.set_cmap('hot')matplotlib.pyplot.set_cmap('jet')
# facecolor颜色映射hexHtml = ['#d73027','#f46d43','#fdae61',          '#fee090','#ffffbf','#e0f3f8',          '#abd9e9','#74add1','#4575b3']sample = 10000fig,ax = plt.subplots(1,1)for j in range(len(hexHtml)):    y = np.random.normal(0,.1,size=sample).cumsum()    x = np.arange(sample)    ax.scatter(x,y,              label=str(j),              linewidths=.2,              edgecolors='grey',              facecolor=hexHtml[j])ax.legend()plt.show()
# pcolor()rd = np.random.rand(10,10)plt.pcolor(rd,cmap='BuPu')plt.colorbar()plt.show()
# cmap=mpl.cm.BuPua = np.random.randn(100)b = np.random.randn(100)exponent = 2plt.subplot(131)plt.scatter(a,b,np.sqrt(np.power(a,exponent)+np.power(b,exponent))*100,           c=np.random.rand(100),           cmap=mpl.cm.jet,           marker='o',           zorder=1)plt.subplot(132)plt.scatter(a,b,50,marker='o',zorder=10)plt.subplot(133)plt.scatter(a,b,50,           c=np.random.rand(100),           cmap=mpl.cm.BuPu,           marker='+',           zorder=100)plt.show()
# 极坐标颜色设置barSlices = 12theta = np.linspace(0,2*np.pi,barSlices,endpoint=False)radii = 30*np.random.rand(barSlices)width = np.pi/4*np.random.rand(barSlices)fig = plt.figure()ax = fig.add_subplot(111,polar=True)bars = ax.bar(theta,radii,width=width,bottom=0)for r,bar in zip(radii,bars):    bar.set_facecolor(mpl.cm.Accent(r/30))    bar.set_alpha(r/30)    plt.show()
# 等高线颜色s = np.linspace(-.5,.5,1000)x,y = np.meshgrid(s,s)fig,zx = plt.subplots(1,1)z = x**2+y**2+np.power(x**2+y**2,2)cs = plt.contour(x,y,z,cmap=mpl.cm.hot)plt.clabel(cs,fmt='%3.2f')plt.colorbar(cs)plt.show()
# 色卡import scipy.miscascent = scipy.misc.ascent()plt.imshow(ascent,cmap=mpl.cm.gray)plt.colorbar()plt.show()

图片保存

plt.scatter(np.random.rand(100),np.random.rand(100))plt.savefig("demo.png")plt.show()

标签: #python输出带颜色的文字