<jupyter_start><jupyter_text>Making publication ready figures with matplotlib`Authors:<NAME>`<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import mne
from mne.datasets import sample
mne.set_log_level('warning')
data_path = sample.data_path()
evoked_fname = data_path + '/MEG/sample/sample_audvis-ave.fif'
evoked = mne.read_evokeds(evoked_fname, condition='Left Auditory', baseline=(None, 0))
evoked.pick_types(meg='grad')
evoked.plot();<jupyter_output><empty_output><jupyter_text>Set the figure size, add t=0 vertical line, add text<jupyter_code>def my_plot():
    fig, ax = plt.subplots(1, 1, figsize=(6, 3))
    evoked.pick_types(meg='grad')
    evoked.plot(axes=ax, show=False)
    ax.axvline(0., color='r');
    ax.text(x=0.02, y=0.05, s='Left Auditory', transform=ax.transAxes)

my_plot()<jupyter_output><empty_output><jupyter_text>Change matplotlib style using seaborn<jupyter_code>import seaborn as sns
def set_style():
    # This sets reasonable defaults for font size for
    # a figure that will go in a paper
    sns.set_context("paper")
    # Set the font to be serif, rather than sans
    sns.set(font='serif', font_scale=1.2)
    sns.set_palette('muted')
    # Make the background white, and specify the
    # specific font family
    sns.set_style("whitegrid", {
        "font.family": "serif",
        "font.serif": ["Times", "Palatino", "serif"]
    })


set_style()
my_plot()<jupyter_output><empty_output><jupyter_text>Fine customization of your plotsWhat have every text in magenta? and the lines in red?Use the `findobj` figure method.<jupyter_code>import matplotlib as mpl

sns.set_style("white")

fig = evoked.plot(show=False)  # butterfly plots


fig.subplots_adjust(hspace=1.0)
for text in fig.findobj(mpl.text.Text):
    text.set_fontsize(18)
    text.set_color('blue')

for line in fig.findobj(mpl.lines.Line2D):
    line.set_color('yellow')<jupyter_output><empty_output><jupyter_text>Teaser on seaborn with Pandas<jupyter_code>import numpy as np
import pandas as pd
evoked_peak = evoked.copy().crop(0.07, 0.1)
data = evoked_peak.data
peaks = np.max(data, axis=1)
peaks.shape
latencies = evoked_peak.times[np.argmax(data, axis=1)]
latencies.shape
ch_names = evoked.ch_names
len(ch_names)
layout = mne.find_layout(evoked.info)
layout
idx = [k for k, n in enumerate(layout.names) if n in ch_names]
len(idx)
pos_x, pos_y, _, _ = layout.pos[idx].T
pos_x.shape
df = pd.DataFrame(dict(ch_names=ch_names, peaks=1e13 * peaks,
                       latencies=1e3 * latencies,
                       pos_x=pos_x, pos_y=pos_y))
df.head()
df.peaks.describe()
sns.set_palette('muted')
df.plot.scatter('pos_x', 'pos_y')
sns.lmplot(x="latencies", y="peaks", data=df);
sns.set_style("whitegrid")
sns.lmplot(x="latencies", y="pos_x", data=df[df.peaks > 50]);
m = np.mean(df.pos_x)
print(m)
df['laterality'] = [('Left' if p < m else 'Right') for p in df.pos_x]
sns.set_style("whitegrid")
sns.set_palette('Set2')
sns.barplot(data=df, x='laterality', y='latencies');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14162806088682992, 'fraction_numerical': 0.01158173395102581, 'mean_word_length': 5.18200408997955, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '666200', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1194, 'n_tokens_neox': 1148, 'n_words': 239, 'path': '2016_05_Halifax/Day3/matplotlib_figures.ipynb', 'repo_name': 'mne-tools/mne-workshops', 'size': '3034'}