Matplotlib for presenting results(论文画图matplotlib jupyter文档)

2023-12-12 07:58

本文主要是介绍Matplotlib for presenting results(论文画图matplotlib jupyter文档),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

参考jupyter的官方文档点击打开链接

第一部分讲了matplotlib 自带的各种风格,可以画出不同 背景样式的图

第二部分讲的是绘制heatmap,利用heatmap可以将自己的结果和别的模型进对比(用不同深重颜色代表差异)

第三部分利用TSNE这个库可以把高维数据表示为二维图中(没看懂)

第四部分画堆叠bar图


Learning curves¶

Make matplotlib graphics to show up inline.

In [8]:
%matplotlib inline

Import matplotlib. If you want to generate images without having a window appear (if you run your scripts on servers), use a non-interactive backend such as Agg (for PNGs), PDF, SVG or PS. To do so, uncomment the second line in the following cell.

In [9]:
import matplotlib
#matplotlib.use('Agg')
import matplotlib.pyplot as plt

Matplotlib has different styles. Run the cell to check which styles are available.

In [10]:
plt.style.available
Out[10]:
[u'seaborn-darkgrid',u'Solarize_Light2',u'seaborn-notebook',u'classic',u'seaborn-ticks',u'grayscale',u'bmh',u'seaborn-talk',u'dark_background',u'ggplot',u'fivethirtyeight',u'_classic_test',u'seaborn-colorblind',u'seaborn-deep',u'seaborn-whitegrid',u'seaborn-bright',u'seaborn-poster',u'seaborn-muted',u'seaborn-paper',u'seaborn-white',u'fast',u'seaborn-pastel',u'seaborn-dark',u'seaborn',u'seaborn-dark-palette']

To set a style you want use stlye.use.

In [11]:
matplotlib.style.use('seaborn-darkgrid')

plot_learning_curves plots train, dev, test measure-time curves. Play around with different parameters to get a figure that suits you the best. flist is the list of size 3; the first element is the list of train scores, the second of dev scors and the third of test scores.

In [12]:
def plot_learning_curves(fig_path, n_epochs, flist, style=''):measure = 'f1'steps_measure = 'epochs'plt.figure(dpi=400)plt.rcParams['font.size'] = 10plt.rcParams['axes.labelsize'] = 12plt.rcParams['axes.labelweight'] = 'bold'plt.rcParams['axes.titlesize'] = 12plt.rcParams['xtick.labelsize'] = 10plt.rcParams['ytick.labelsize'] = 10plt.rcParams['legend.fontsize'] = 10plt.rcParams['figure.titlesize'] = 12steps = range(1, n_epochs+1)plt.title('learning curves' + style)plt.plot(steps, flist[0], linewidth=1, color='#6699ff', linestyle='-', marker='o',markeredgecolor='black',markeredgewidth=0.5, label='train')plt.plot(steps, flist[1], linewidth=3, color='#ff4d4d', linestyle='-', marker='D',markeredgecolor='black',markeredgewidth=0.5, label='test')plt.plot(steps, flist[2], linewidth=2, color='#ffcc66', linestyle='-', marker='s',markeredgecolor='black',markeredgewidth=0.5, label='dev')plt.xlabel(steps_measure)plt.xticks(steps)plt.ylabel(measure)plt.legend(loc='best', numpoints=1, fancybox=True)plt.show()plt.savefig(fig_path)

Let's generate a random examples to illustrate one figure with learning curves.

In [16]:
fig_path = 'figs/'
train_f1 = [0.2, 0.3, 0.4, 0.5, 0.6, 0.62, 0.65, 0.67, 0.68, 0.67, 0.69, 0.72, 0.721, 0.719, 0.72]
dev_f1 = [0.1, 0.2, 0.3, 0.4, 0.5, 0.55, 0.57, 0.59, 0.6, 0.62, 0.61, 0.615, 0.614, 0.6159, 0.62]
test_f1 = [0.05, 0.15, 0.2, 0.25, 0.3, 0.4, 0.45, 0.43, 0.5, 0.51, 0.55, 0.52, 0.53, 0.525, 0.531]
flist = [train_f1, dev_f1, test_f1]
n_epochs = 15
plot_learning_curves(fig_path + 'learning_curve.png', n_epochs , flist)
<matplotlib.figure.Figure at 0x1074e1e50>

And the same figure with all available styles.

In [17]:
for st in plt.style.available:matplotlib.style.use(st)plot_learning_curves(fig_path + 'learning_curve.png', n_epochs , flist, '--' + st)
<matplotlib.figure.Figure at 0x107517250>
<matplotlib.figure.Figure at 0x10a271f10>
<matplotlib.figure.Figure at 0x10a246790>
<matplotlib.figure.Figure at 0x10a878750>
<matplotlib.figure.Figure at 0x10af8a850>
<matplotlib.figure.Figure at 0x107254ad0>
<matplotlib.figure.Figure at 0x10af91ad0>
<matplotlib.figure.Figure at 0x10a8afc10>
<matplotlib.figure.Figure at 0x10bfd8d10>
<matplotlib.figure.Figure at 0x10a583210>
<matplotlib.figure.Figure at 0x10a9d4cd0>
<matplotlib.figure.Figure at 0x10a749d10>
<matplotlib.figure.Figure at 0x10a6573d0>
<matplotlib.figure.Figure at 0x10a5f3f10>
<matplotlib.figure.Figure at 0x10a1ec310>
<matplotlib.figure.Figure at 0x10a118d90>
<matplotlib.figure.Figure at 0x10af57110>
<matplotlib.figure.Figure at 0x10cf635d0>
<matplotlib.figure.Figure at 0x10a9c7fd0>
<matplotlib.figure.Figure at 0x10a1cc550>
<matplotlib.figure.Figure at 0x10a986590>
<matplotlib.figure.Figure at 0x10a 
<matplotlib.figure.Figure at 0x10a300f10>
<matplotlib.figure.Figure at 0x10a21ee90>
<matplotlib.figure.Figure at 0x10bfd1e90>

In your scripts after every epoch or after every 1K iterations evaluate your model on train, dev and test data and append corresponding scripts.

Heatmaps¶

Visualize LSTM outputs¶

Now import prettyplotlib.

In [18]:
import prettyplotlib as ppl

visu_lstm_outputs illustrates a heatmap of LSTM outputs. More examples can be found here: https://github.com/olgabot/prettyplotlib/wiki/Examples-with-code.

In [19]:
def visu_lstm_outputs(fig_path, LSTM_outputs, sentence_tokenized):plt.rcParams['xtick.labelsize'] = 20plt.rcParams['ytick.labelsize'] = 20slen = len(sentence_tokenized)fig, ax = ppl.subplots(1)fig.set_figheight(15)fig.set_figwidth(20)ppl.pcolormesh(fig, ax, LSTM_outputs)ax.set_xticks(np.arange(0.5, slen + 0.5, 1))ax.set_xticklabels(sentence_tokenized)ax.set_title('lstm outputs', fontsize = 25)plt.show()fig.savefig(fig_path)

Let's see how it works for a randomly generated example.

In [20]:
import numpy as npsentence = 'today is a beautiful day'
sentence_tokenized = sentence.split(' ')
slen = len(sentence_tokenized)
LSTM_hidden_size = 15
LSTM_outputs = np.random.rand(LSTM_hidden_size, slen) fig_path = "figs/heatmap_lstm_hidden.png"
visu_lstm_outputs(fig_path, LSTM_outputs, sentence_tokenized)
/Users/ana/tf_cpu/lib/python2.7/site-packages/matplotlib/__init__.py:800: MatplotlibDeprecationWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.mplDeprecation)

For a tensorflow model you need to retrieve ourputs with outputs_op = graph.get_operation_by_name(op_name).outputs[0], run the operation outputs = session.run(outputs_op) and transpose them np.asarray(outputs).transpose().

Visualize improvements¶

If you compare your models with another model, you can visualize improvements and make your results table easier to interpret.

In [21]:
matplotlib.style.use('seaborn-white')

visu_imporovements makes a heatmap of the improvements data and writes improvements values in the center of cells.

In [22]:
def visu_improvements(improvements_data):y_labels = ['model1', 'model2', 'model3', 'model4']x_labels = ['measure1', 'measure2', 'measure3']fig, ax = plt.subplots(1)fig.set_figheight(15)fig.set_figwidth(30)#plt.pcolormesh(fig, ax, my_data)plt.pcolor(improvements_data, cmap=plt.cm.YlGn)ax.set_aspect('auto')for y in range(improvements_data.shape[0]):for x in range(improvements_data.shape[1]):plt.text(x + 0.5, y + 0.5, '%.2f' % improvements_data[y, x],horizontalalignment='center',verticalalignment='center',size=30,weight='bold')ax.set_yticks(np.arange(0.5, len(y_labels) + 0.5, 1))ax.set_yticklabels(y_labels, size=25, weight='bold')ax.set_xticks(np.arange(0.5, len(x_labels) + 0.5, 1))ax.set_xticklabels(x_labels, size=17, weight='bold')ax.tick_params(axis='both', labelsize=25)ax.set_title('improvements', fontsize = 40)cbar = plt.colorbar()cbar.ax.tick_params(labelsize=30)fig.savefig(fig_path)
Make a random example.
In [23]:
fig_path = "improvements.png"
n_models = 4, 
n_measures = 3
improvements_data = np.random.random_sample((4,3))
np.savetxt(fname='improvements.txt', X=improvements_data, fmt='%10.5f', delimiter='\t')
visu_improvements(improvements_data)

You will write differences of your models and another model in a text file (one row for one model) and read it as a numpy array.

In [24]:
improvements_data = np.loadtxt('improvements.txt', delimiter='\t')
visu_improvements(improvements_data)

tSNE¶

Using tSNE you can visualize your high-dimensional vectors in 2D space. Learn first how to use tSNE effectively https://distill.pub/2016/misread-tsne/.

In [25]:
def plot(candidates_tsne, n_classes, tags, ids, file_path):almost_black = '#262626'fig, ax = plt.subplots(1)# hard-coded for the example with 3 classescolors = ['red', 'blue']for k in range(n_classes):begin = sum(sizes[:k])end = sum(sizes[:k]) + sizes[k]x = [candidates_tsne[i, 0] for i in range(begin, end)]y = [candidates_tsne[i, 1] for i in range(begin, end)]# marks every vector with "id (tag)", where id and tag could be anything you like, e.g. word (POS)# every vector point is colored with the corresponding class colortext = [str(ids[i]) + " (" + str(tags[i]) + ")" for i in range(begin, end)]ax.scatter(x, y, label='class'+str(k+1), alpha=0.5, edgecolor=almost_black, facecolor=colors[k], linewidth=0.15)for i, txt in enumerate(text):ax.annotate(txt, (x[i], y[i]))# remove top and right axesspines_to_remove = ['top', 'right']for spine in spines_to_remove:ax.spines[spine].set_visible(False)ax.xaxis.set_ticks_position('none')ax.yaxis.set_ticks_position('none')spines_to_keep = ['bottom', 'left']for spine in spines_to_keep:ax.spines[spine].set_linewidth(0.5)ax.spines[spine].set_color(almost_black)# make axis almost blackax.xaxis.label.set_color(almost_black)ax.yaxis.label.set_color(almost_black)ax.set_xticks([])ax.set_yticks([])ax.title.set_color(almost_black)ax.set_title('tsne', fontsize = 20)# make the legend background light graylight_grey = np.array([float(248)/float(255)]*3)legend = ax.legend(frameon=True, scatterpoints=1)rect = legend.get_frame()rect.set_facecolor(light_grey)rect.set_linewidth(0.0)# change the legend label colors to almost blacktexts = legend.textsfor t in texts:t.set_color(almost_black)ax.grid(False)plt.show()fig.savefig(str(file_path), dpi=200)plt.close()

Make random classification dataset.

In [26]:
import sklearn
from sklearn import datasetsn_classes = 2
X, y = datasets.make_classification(n_samples=10, n_features=20, class_sep=10)
types = [[] for _ in range(n_classes)]
sizes = [0]*n_classes
for i, l in enumerate(y):types[l].append(X[i])sizes[l] += 1for i in range(n_classes):types[i] = np.asarray(types[i]).reshape((sizes[i], 20))

And plot.

In [27]:
from sklearn.manifold import TSNEjoint = np.concatenate(types, 0)
tsne = TSNE(init='pca', n_iter=5000)
candidates_tsne = tsne.fit_transform(joint)
file_path = "figs/tsne.png"
tags = [1]*sizes[0] + [2]*sizes[1]
ids = range(sum(sizes))
plot(candidates_tsne, n_classes, tags, ids, file_path)

When running on serves comment plt.show for all examples.

Stacked bars plot¶

In [30]:
#from matplotlib.font_manager import FontProperties
from operator import add
In [91]:
def tags_stacked_bars(distr1, distr2):distr1.sort(key=lambda item: item[1], reverse=True)distr1_dict = dict(distr1)distr2.sort(key=lambda item: item[1], reverse=True)distr2_dict = dict(distr2)# plot tags which has positive frequency in the first distribution and higher than one in the secondplot_tags = [tag for tag, freq in distr1 if freq > 0.0]other = []for tag, freq in distr2:if freq > 1.0 and tag not in plot_tags:plot_tags.append(tag)else:other.append(freq)distr1_pruned_dict = {}for key in plot_tags:if key in distr1_dict:distr1_pruned_dict[key] = distr1_dict[key]else:distr1_pruned_dict[key] = 0distr2_pruned_dict = {}for key in plot_tags:if key in distr2_dict:distr2_pruned_dict[key] = distr2_dict[key]else:distr2_pruned_dict[key] = 0distr1_freq = []distr2_freq = []for tag in plot_tags:distr1_freq.append(distr1_pruned_dict[tag])distr2_freq.append(distr2_pruned_dict[tag])plot_tags.append("other")distr1_freq.append(0.0)distr2_freq.append(np.mean(np.asarray(other)))# plot stacked barwidth = 0.4height_cumulative = [0.0, 0.0]plots = []ind = [0, 0.5]fig = plt.figure(figsize=(11, 11))ax = fig.add_subplot(1, 1, 1)colors = ['#0066ff', '#ffcc99', '#adebad', '#ff5c33', '#ac3973', '#ffbf00', '#7979d2', '#00cc99']for k in range(len(plot_tags)):if k < 8:color = colors[k]else:color = np.random.rand(3)if k == 0:plots.append(ax.bar(ind, [distr1_freq[k], distr2_freq[k]], width, color=color, edgecolor='black'))else:plots.append(ax.bar(ind, [distr1_freq[k], distr2_freq[k]], width,bottom=height_cumulative, color=color, edgecolor='black'))height_cumulative = map(add, [distr1_freq[k], distr2_freq[k]], height_cumulative)plt.ylabel('median occurrence of a tag', fontsize=20)title = 'distribution of tags'plt.title(title, fontsize=25)plt.xticks([ind[0]+width/2.0, ind[1]+width/2], ["distr1", "distr2"], fontsize=20)plt.yticks(np.arange(0, sum(distr2_freq), 3))handles = [p[0] for p in plots]plt.legend(handles[::-1], plot_tags[::-1], prop={'size': 10}, loc='center left', bbox_to_anchor=(1, 0.5))fig_path = "figs/stacked_bars.png"plt.show()fig.savefig(fig_path)
In [92]:
distr1 = [["NN", 5.0], ["NP", 3.0], ["PP", 2.0], ["ADV", 2.0], ["S", 2.0], ["ART", 2.0], ["$.", 1.0], ["KON", 1.0], ["ADJA", 1.0], ["APPR", 1.0], ["VVINF", 1.0], ["VVFIN", 1.0], ["$,", 1.0], ["KOUS", 1.0], ["VP", 1.0], ["VAFIN", 1.0], ["PPER", 1.0], ["CVZ", 0.0], ["AVP", 0.0], ["CAP", 0.0], ["CVP", 0.0], ["NE", 0.0], ["CPP", 0.0], ["VMFIN", 0.0], ["PTKNEG", 0.0], ["VAPP", 0.0], ["APPO", 0.0], ["PRF", 0.0], ["VVIZU", 0.0], ["NM", 0.0], ["PDAT", 0.0], ["PIAT", 0.0], ["ADJD", 0.0], ["$[", 0.0], ["PTKVZ", 0.0], ["PRELS", 0.0], ["PIS", 0.0], ["ROOT", 0.0], ["PROAV", 0.0], ["APZR", 0.0], ["PPOSAT", 0.0], ["CO", 0.0], ["CNP", 0.0], ["PDS", 0.0], ["VVPP", 0.0], ["AP", 0.0], ["XY", 0.0], ["PWAV", 0.0], ["CS", 0.0], ["PTKANT", 0.0], ["VZ", 0.0], ["PTKZU", 0.0], ["CARD", 0.0], ["PWS", 0.0], ["VMINF", 0.0], ["MPN", 0.0], ["VAINF", 0.0], ["APPRART", 0.0], ["KOKOM", 0.0], ["PTKA", 0.0], ["PIDAT", 0.0], ["TRUNC", 0.0], ["KOUI", 0.0], ["CAVP", 0.0]]
distr2 = [["NN", 10.0], ["NP", 7.0], ["S", 7.0], ["PP", 5.0], ["ADV", 4.0], ["ADJA", 3.0], ["APPR", 3.0], ["VP", 3.0], ["ART", 3.0], ["VVINF", 2.0], ["VVFIN", 2.0], ["VAFIN", 2.0], ["PPER", 2.0], ["KON", 1.0], ["VMFIN", 1.0], ["ADJD", 1.0], ["PTKNEG", 1.0], ["$.", 1.0], ["$,", 1.0], ["CNP", 1.0], ["KOUS", 1.0], ["PDS", 1.0], ["VVPP", 1.0], ["AP", 1.0], ["CS", 1.0], ["APPRART", 1.0], ["CVZ", 0.0], ["AVP", 0.0], ["CAP", 0.0], ["PWAT", 0.0], ["CVP", 0.0], ["NE", 0.0], ["CPP", 0.0], ["VAPP", 0.0], ["APPO", 0.0], ["PRF", 0.0], ["VVIZU", 0.0], ["NM", 0.0], ["PDAT", 0.0], ["PIAT", 0.0], ["FM", 0.0], ["$[", 0.0], ["PTKVZ", 0.0], ["PRELS", 0.0], ["PIS", 0.0], ["ROOT", 0.0], ["PROAV", 0.0], ["TRUNC", 0.0], ["PPOSAT", 0.0], ["CO", 0.0], ["XY", 0.0], ["PWAV", 0.0], ["PTKANT", 0.0], ["VZ", 0.0], ["PTKZU", 0.0], ["CARD", 0.0], ["PWS", 0.0], ["PRELAT", 0.0], ["VVIMP", 0.0], ["VMINF", 0.0], ["MPN", 0.0], ["VAINF", 0.0], ["KOKOM", 0.0], ["PTKA", 0.0], ["PIDAT", 0.0], ["APZR", 0.0], ["KOUI", 0.0], ["CAVP", 0.0]]tags_stacked_bars(distr1, distr2)


这篇关于Matplotlib for presenting results(论文画图matplotlib jupyter文档)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/483834

相关文章

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Python实现word文档内容智能提取以及合成

《Python实现word文档内容智能提取以及合成》这篇文章主要为大家详细介绍了如何使用Python实现从10个左右的docx文档中抽取内容,再调整语言风格后生成新的文档,感兴趣的小伙伴可以了解一下... 目录核心思路技术路径实现步骤阶段一:准备工作阶段二:内容提取 (python 脚本)阶段三:语言风格调

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

详解C#如何提取PDF文档中的图片

《详解C#如何提取PDF文档中的图片》提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使用,下面我们就来看看如何使用C#通过代码从PDF文档中提取图片吧... 当 PDF 文件中包含有价值的图片,如艺术画作、设计素材、报告图表等,提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使

Python实现合并与拆分多个PDF文档中的指定页

《Python实现合并与拆分多个PDF文档中的指定页》这篇文章主要为大家详细介绍了如何使用Python实现将多个PDF文档中的指定页合并生成新的PDF以及拆分PDF,感兴趣的小伙伴可以参考一下... 安装所需要的库pip install PyPDF2 -i https://pypi.tuna.tsingh

Python批量调整Word文档中的字体、段落间距及格式

《Python批量调整Word文档中的字体、段落间距及格式》这篇文章主要为大家详细介绍了如何使用Python的docx库来批量处理Word文档,包括设置首行缩进、字体、字号、行间距、段落对齐方式等,需... 目录关键代码一级标题设置  正文设置完整代码运行结果最近关于批处理格式的问题我查了很多资料,但是都没