python matplotlib常见图形画法

import matplotlib.pylab import plt
fig=plt.figure()  #可以设置画布的大小,以及分辨率
matplotlib对象都位于Figure对象中,plt.subplots,它可以创建一个新的Figure,且返回一个含有已创建的subplot对象的numpy数组。
1、刻度尺的设置

import numpy as np
from matplotlib.pylab import plt
from matplotlib.pylab import *
from matplotlib.ticker import MultipleLocator    #设置
from matplotlib.ticker import FormatStrFormatter
# 将x轴的主标签设置为20的倍数
xmajorLocator=MultipleLocator(20)
# 设置x轴标签文本的格式
# %1.0f表示:打印1位有效数字(若超出,则按该类型实际有效位显示),其中小数数位为
# %m.nf表示输出m位浮点型数,其中,m为域宽(整数位数+小数位数+小数点),n为小数位数(自动对n位后小数进行四舍五入)或n个字符;若输出数本身的长度小于m,则左边补空格,即为右对齐的方式
xmajorFormatter=FormatStrFormatter('%3.1f')
#将x轴次刻度标签设置为5的倍数
xminorLocator=MultipleLocator(5)
#设定y 轴的主刻度间隔及相应的刻度间隔显示格式
##将y轴主刻度标签设置为1.0的倍数
ymajorLocator = MultipleLocator(1.0)
#设置y轴标签文本的格式
ymajorFormatter = FormatStrFormatter('%1.1f')
#将此y轴次刻度标签设置为0.2的倍数
yminorLocator = MultipleLocator(0.2)
t=np.arange(1.0, 100.0, 1)
s=t*exp(-t*1.3)+2*sqrt(t)
ax =subplot(111)  #布置一个画布
plt.plot(t,s,'--r*')
#设置主刻度标签的位置,标签文本的格式
ax.xaxis.set_major_locator(xmajorLocator)
ax.xaxis.set_major_formatter(xmajorFormatter)
ax.yaxis.set_major_locator(ymajorLocator)
ax.yaxis.set_major_formatter(ymajorFormatter)
#显示次刻度标签的位置,没有标签文本
ax.xaxis.set_minor_locator(xminorLocator)
ax.yaxis.set_minor_locator(yminorLocator)
ax.xaxis.grid(True, which='major') #x坐标轴的网格使用主刻度
ax.yaxis.grid(True, which='minor') #y坐标轴的网格使用次刻度
show()

2、刻度尺的标签设置
旋转刻度尺设置:
plt.xticks(rotation=30)#x标签选择30度

字体设置:字体样式的设计中可接受字典

font2 = {'family': 'Times New Roman',
         'weight': 'normal',
         'size': 30,
         }

plt.xlabel('round', font2)
plt.ylabel('value',fontsize=30)

标签设置:
文本显示:plt.text(x轴位置,y轴位置,显示内容)
显示值:
其中a和b为x轴、y轴分别传入的值
for a, b in zip(x1, y1):
    plt.text(a, b, b, ha='center', va='bottom', fontsize=20)
plt.legend()
显示栅格:plt.grid(True)
##饼图
import matplotlib.pyplot as plt
#设置标签
labels= 'A','B','C','D'
fracs = [15,20,35,30]
#axes控制 X Y轴变成1:1
plt.axes(aspect = 1)
explode =[0,0,0.06,0.05]
##autopct ='%.0f%%'是将百分比(0表示取零位小数)显示出来,explode= explode 是将饼图部分凸显出来。#shadow 显示阴影。
plt.pie(x=fracs,labels=labels,autopct='%.0f%%',explode = explode, shadow = True)
plt.show()

##箱体图
import numpy as np
from matplotlib.pylab import plt
#当需要更改随机数,只需要更改seed()里面的值,seed()值代表某一特定的随机数,更改后如需返回,只需更改seed()值
np.random.seed(100)
data=np.random.normal(size=1000,loc=0,scale=1)#产生1000个随机数,平均数为0,scale = 方差
plt.boxplot(data,sym='o',whis=1.0)#产生1000个随机数,平均数为0,scale = 方差
plt.savefig('/root/1.png',bbox_inches='tight')#bbox_inches是控制保存图片的时候自动去除空白部分
plt.show()
#散点图
import numpy as np
from matplotlib.pylab import plt
height =[161,170,182,175,173,165]
weight = [50,58,80,70,69,55]
plt.scatter(height, weight)
plt.show()
N =1000
x = np.random.randn(N)
y = x+np.random.randn(N)*0.7
plt.scatter(x,y)
plt.show()

import matplotlib.pyplot as plt
#颜色映射是用于突出数据的规律
x_values=list(range(1,1001))
y_values=[x**2 for x in x_values]
plt.scatter(x_values,y_values,c=y_values,cmap=plt.cm.Blues,edgecolors='none',s=40)
plt.show()
# 柱状图
import numpy as np
from matplotlib.pylab import plt
N = 5
y = [20,10,30,25,15]
index = np.arange(N)
#直立柱状图
plt.bar(left = index, height = y, color ='red', width =0.5)
#横立柱状图
plt.bar(left = 0, bottom = index, width = y, color ='red',height=0.5, orientation = 'horizontal')
plt.show()
#两个柱状图一起画会出现重叠
#第一个值是位置,第二个是值,宽度等
index = np.arange(4)
sales_BJ = [52, 55, 63, 53]
sales_SH = [44, 66, 55, 41]
bar_width = 0.3
plt.bar(index, sales_BJ, bar_width, color='b',label='sales_BJ')
plt.bar(index+bar_width, sales_SH, bar_width, color ='r',label='sales_SH')
#plt.bar(index, sales_SH, bar_width, color='r', bottom=sales_BJ)
plt.legend()
plt.show()
 

# 折方图
import numpy as np
from matplotlib.pylab import plt
mu =100
sigma =20
np.random.seed(8)
x = mu + sigma*np.random.randn(20000)
plt.hist(x,bins=100, color ='red',normed =True)
plt.show()
'''参数说明:
bins:指定直方图条形的个数
normed:标准化,是否将直方图的频数转换成频率
histtype:指定直方图的类型,默认为bar,还有barstacked/sted/stepfilledorientation:水平还是垂直【'horizontal','vertical'】默认为垂直方向
align:设置条形边界值的对其方式,默认为mid,还有'left','right'
color:设置直方图颜色label:设置直方图的标签,可通过legend展示其图例;其它:
stacked:当有多个数据时,是否需要将直方图呈堆叠摆放,默认水平摆放
log:是否需要对绘图数据进行log变换rwidth:设置直方图条形宽度的百分比
bottom:可以为直方图的每个条形添加基准线,默认为0
facecolor:长条形的颜色
edgecolor:长条形边框的颜色
alpha:透明度
'''
3、轴的设置
#设置共轴,并设置轴的大小
plt.tick_params(axis='both',which='major',labelsize=16)
4、pygal下的gui标签gui
import pygal
hist=pygal.bar()
hist.add('D6',data)
5、隐藏x轴、y轴
plt.axes().get_xaxis().set_visible(False)
plt.axes().get_yaxis().set_visible(False)
6、颜色的打色
plt.scatter(x,y,c=y,cmp=plt.cm.Blues,edgecolor='none',s=15)

7、中间部分填充颜色
plt.fill_between(x,y_max,y_min,facecolor='blue',alpha=0.1)
 

#https://blog.csdn.net/weixin_30935137/article/details/80670872

猜你喜欢

转载自blog.csdn.net/qq_24726509/article/details/83217830