概述
画图效果
CDF画图举例
# encoding=utf-8
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
import copy
"""
Devl env: python3, matplotlib;
Developer: zengyue;
Encoding: utf-8.
"""
class Draw():
"""Function: Draw line chart, bar chart, column chart, cumulative distribution function chart."""
def __init__(self, fig_type='eps', font_family='Times New Roman', font_size=20, fig_size = (6.4,4.8), line_width=4,
fig_left=0.15, fig_right=0.9, fig_top=0.9, fig_bottom= 0.2 , agg_fig=False, show_fig=True, show_grid=True,
show_title=False):
# reset
matplotlib.use('TkAgg')
#record figure typ
self.fig_type = fig_type
#set fix paramenters: color, mec, marer, marker, hatch, line style
self.colors = ['red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'black', 'white']
#default colors
self.mecs = self.colors
#the edge color of markers
self.mfcs = 'white'
#the interner color of markers
self.edge_color = 'white'
self.markers = ['o', '^', '.', '*', '-', '+', 'x', 'O']
#markers on each line
self.hatchs = self.markers
#hatches
self.line_style = ['-', '-.', ':', '--', '-:']
#setting default paramenters
plt.rcParams['font.family'] = 'Times New Roman'
plt.rcParams['font.size'] = font_size
plt.rcParams['figure.figsize'] = fig_size
plt.rcParams['lines.linewidth'] = line_width
plt.rcParams['figure.subplot.left'] = fig_left
plt.rcParams['figure.subplot.right'] = fig_right
plt.rcParams['figure.subplot.top'] = fig_top
plt.rcParams['figure.subplot.bottom'] = fig_bottom
#show or not
self.show_fig = show_fig
self.show_grid = show_grid
self.show_title = show_title
#enable execution on systems without a GUI graphical interface
if agg_fig:
plt.switch_backend('agg')
def cdfChart(self, xData, yData, lineLabels, figName, xLabel, yLabel='CDF', xLowBound=0, xUpBound=100,
yLowBound=0, yUpBound=1):
ax=plt.gca()
ax.set_xscale('log')
len_data = len(xData)
#'''
for i in range(0,len_data):
#insert start value
#xData[i].insert(0,0)
#yData[i].insert(0,0)
#insert end value
xData[i].append(xUpBound)
yData[i].append(1)
print(i,len(xData[i]),len(yData[i]))
#'''
for i in range(0,len_data):
plt.plot(xData[i], yData[i], linestyle=self.line_style[i], color=self.colors[i], drawstyle='steps-post',
label=lineLabels[i])
plt.xlim(xLowBound, xUpBound)
plt.ylim(yLowBound, yUpBound)
plt.xlabel(xLabel)
plt.ylabel(yLabel)
plt.xticks()
plt.yticks()
plt.legend()
plt.grid(self.show_grid)
if self.show_title:
plt.title(title_name, fontsize=self.font_size)
plt.savefig(figName+"."+self.fig_type)
if self.show_fig:
plt.show()
plt.close()
def cdfExample():
#initiate
newDraw = Draw(agg_fig=False)
#CDF chart example
lineLabels = ['ALG-1','ALG-2']
xdata1 = [1,2,3,4,5,6,7]
xdata2 = [2,3,4,5,6,7,8]
xData = []
xData.append(xdata1)
xData.append(xdata2)
yData = []
for i in range(0,len(xData)):
data = []
for j in range(1,len(xData[i])+1):
data.append(j/len(xData[i]))
yData.append(data)
for i in range(0,len(xData)):
xData[i].insert(0,0)
yData[i].insert(0,0)
newDraw.cdfChart(xData, yData, lineLabels=lineLabels, figName="Fig-Default", xLabel="xLabel", yLabel='CDF', xLowBound=0, xUpBound=100*max(xdata2),
yLowBound=0, yUpBound=1)
if __name__=='__main__':
cdfExample()
最后
以上就是迅速朋友为你收集整理的Matplotlab画CDF图的全部内容,希望文章能够帮你解决Matplotlab画CDF图所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复