Skip to content

Commit d48683b

Browse files
committed
WIP: Improve plot legend
1 parent b038042 commit d48683b

2 files changed

Lines changed: 65 additions & 23 deletions

File tree

notebooks/mesonic-dev-timeline-plot.ipynb

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,6 @@
2020
"%autoreload 2"
2121
]
2222
},
23-
{
24-
"cell_type": "code",
25-
"execution_count": null,
26-
"id": "1b446b60",
27-
"metadata": {},
28-
"outputs": [],
29-
"source": [
30-
"%matplotlib widget"
31-
]
32-
},
3323
{
3424
"cell_type": "code",
3525
"execution_count": null,
@@ -54,6 +44,18 @@
5444
"import numpy as np"
5545
]
5646
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": null,
50+
"id": "32724ee3",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"%matplotlib inline\n",
55+
"plt.rcParams['figure.figsize'] = [8, 2]\n",
56+
"plt.rcParams['figure.dpi'] = 100"
57+
]
58+
},
5759
{
5860
"cell_type": "code",
5961
"execution_count": null,
@@ -105,11 +107,12 @@
105107
"source": [
106108
"context.clear() \n",
107109
"with context.at(0.3):\n",
108-
" s2.start(freq=300, amp=0.1)\n",
110+
" s2.start(freq=300, amp=0.1, pan=1)\n",
109111
"for t in range(4,10):\n",
110112
" with context.at(t/10): \n",
111113
" s2.freq = 100 * t\n",
112114
" s2.amp += 0.1\n",
115+
" s2.pan = -1 * s2.pan.value\n",
113116
"with context.at((t+1)/10): \n",
114117
" s2.stop()\n",
115118
"\n",
@@ -168,7 +171,8 @@
168171
"metadata": {},
169172
"outputs": [],
170173
"source": [
171-
"context.timeline.plot_new(offset=\"amp\", width=\"amp\")\n",
174+
"plt.figure()\n",
175+
"context.timeline.plot_new(offset=\"amp\", width=\"freq\")\n",
172176
"#plt.semilogy()"
173177
]
174178
},
@@ -182,6 +186,37 @@
182186
"context.timeline.plot_new(offset=\"freq\", width=\"pan\")"
183187
]
184188
},
189+
{
190+
"cell_type": "code",
191+
"execution_count": null,
192+
"id": "168d3682",
193+
"metadata": {},
194+
"outputs": [],
195+
"source": [
196+
"context.timeline.plot_new(offset=\"pan\", width=\"freq\")"
197+
]
198+
},
199+
{
200+
"cell_type": "code",
201+
"execution_count": null,
202+
"id": "c07e2905",
203+
"metadata": {},
204+
"outputs": [],
205+
"source": [
206+
"def safe_amp_to_db(amp):\n",
207+
" return np.where(amp > 0, pam.amp_to_db(amp), -90)\n"
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"execution_count": null,
213+
"id": "8efdb710",
214+
"metadata": {},
215+
"outputs": [],
216+
"source": [
217+
"safe_amp_to_db(-1)"
218+
]
219+
},
185220
{
186221
"cell_type": "code",
187222
"execution_count": null,

src/mesonic/timeline.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ def plot_new(self, offset="freq", width="amp", scale=None):
573573
# "color": None,
574574
# "y_offset": 0,
575575

576-
fig = plt.figure(figsize=(8, 2))
576+
fig = plt.figure() # figsize=(8, 2)
577577
ax = fig.add_subplot(1, 1, 1)
578578
cmap = plt.get_cmap("Set1")
579579

@@ -608,7 +608,6 @@ def warp_width(value, scale=None, width_min=2, width_max=4):
608608
ret = pam.linlin(
609609
value, widths.min(), widths.max(), width_min, width_max
610610
)
611-
print(value, ret)
612611
return ret
613612

614613
offsets = warp_offset(offsets)
@@ -637,20 +636,30 @@ def warp_width(value, scale=None, width_min=2, width_max=4):
637636
y_min = min(offsets.min(), y_min)
638637
y_max = max(offsets.max(), y_max)
639638

640-
x_lim = (x_max - x_min) * 0.01
641-
ax.set_xlim(x_min - x_lim, x_max + x_lim)
639+
# x_lim = (x_max - x_min) * 0.01
640+
# ax.set_xlim(x_min - x_lim, x_max + x_lim)
642641
# y_lim = (y_max - y_min) * 0.1
643642
# axes.set_ylim(y_min - y_lim, y_max + y_lim)
644643

644+
MAX_LEGEND_NCOLS = 5
645+
645646
# test
646-
ax.legend(handles=patches, loc="best")
647+
ax.legend(
648+
handles=patches,
649+
loc="lower left",
650+
mode="expand",
651+
bbox_to_anchor=(0.0, 1.02, 1.0, 0.102),
652+
borderaxespad=0,
653+
ncol=min(len(patches), MAX_LEGEND_NCOLS),
654+
)
647655
ax.grid()
648-
ax.set_title("Timeline")
649656
ax.set_xlabel("time [s]")
650657

651658
if offset == "freq":
652659
ax.set_yscale("log")
653660
ax.set_ylabel("frequency [Hz]")
661+
662+
# TODO perhaps we also could want mel?
654663
secax = ax.secondary_yaxis(
655664
location="right", functions=(pam.cps_to_midi, pam.midi_to_cps)
656665
)
@@ -664,22 +673,20 @@ def warp_width(value, scale=None, width_min=2, width_max=4):
664673
ax.set_ylabel("amplitude")
665674

666675
def safe_amp_to_db(amp):
667-
return pam.amp_to_db(amp) if amp > 0 else -90
676+
return np.where(amp > 0, pam.amp_to_db(amp), -90)
668677

669678
secax = ax.secondary_yaxis(
670679
location="right", functions=(safe_amp_to_db, pam.db_to_amp)
671680
)
672681
secax.set_ylabel("level [dB]")
673682
secax.yaxis.set_major_locator(
674-
ticker.MaxNLocator(nbins="auto", steps=[1, 2, 4, 5, 10], integer=True)
683+
ticker.MaxNLocator(nbins="auto", steps=[1, 2, 4, 5, 10])
675684
)
676685
secax.yaxis.set_major_formatter(lambda x, pos: str(int(x)))
677686
else:
678687
ax.set_ylabel(offset)
679688

680-
ax.autoscale(enable=True, axis=True, tight=True)
681-
# axes.margins(0.05)
682-
689+
ax.autoscale(enable=True)
683690
fig.tight_layout()
684691

685692
# TODO Idea: convention to order the Synth Params by most probalbe usage

0 commit comments

Comments
 (0)