0todd0000 / spm1d

One-Dimensional Statistical Parametric Mapping in Python
GNU General Public License v3.0
61 stars 21 forks source link

legend #120

Closed yossy-biomech closed 4 years ago

yossy-biomech commented 4 years ago

I would like to put legend on graph in ex_ttest2.py

"pyplot. legend()" was used. But, legend was not appeared.

How can I do it?

ex_ttest2.py

(2) Plot:

pyplot.close('all')

plot mean and SD:

pyplot.figure( figsize=(8, 3.5) ) ax = pyplot.axes( (0.1, 0.15, 0.35, 0.8) ) spm1d.plot.plot_mean_sd(YA) spm1d.plot.plot_mean_sd(YB, linecolor='r', facecolor='r') ax.axhline(y=0, color='k', linestyle=':') ax.set_xlabel('Time (%)') ax.set_ylabel('Plantar arch angle (deg)')

plot SPM results:

ax = pyplot.axes((0.55,0.15,0.35,0.8)) ti.plot() ti.plot_threshold_label(fontsize=8) ti.plot_p_values(size=10, offset_all_clusters=(0,0.9)) ax.set_xlabel('Time (%)') pyplot.show()

0todd0000 commented 4 years ago

spm1d does not support labels or legends directly, so you need to use matplotlib to customize legends.

The most general approach to add a legend is to use artists that are stored in the axes (ax) object, like lines. For example:

pyplot.legend( ax.lines, [ 'label0' , 'label1' ] )

Here is a full working example:

import spm1d
from matplotlib import pyplot as plt

# Load data:
dataset      = spm1d.data.uv1d.t2.PlantarArchAngle()
dataset      = spm1d.data.uv1d.t2.SimulatedTwoLocalMax()
YA,YB        = dataset.get_data()

# Conduct t test:
alpha      = 0.05
t          = spm1d.stats.ttest2(YB, YA, equal_var=True)
ti         = t.inference(alpha, two_tailed=False, interp=True)

# Plot:
plt.close('all')
plt.figure( figsize=(8, 3.5) )
ax = plt.axes( (0.1, 0.15, 0.35, 0.8) )
spm1d.plot.plot_mean_sd(YA)
spm1d.plot.plot_mean_sd(YB, linecolor='r', facecolor='r')

# Add a legend
plt.legend( ax.lines, ['a','b'] )

plt.show()

fig

For more details on customizing legends, see the Legend guide and the Artist tutorial at matplotlib.org