help with plot_posterior_nodes (color and style)

274 views
Skip to first unread message

Mahsa Shalchy

unread,
Feb 19, 2020, 3:49:02 AM2/19/20
to hddm-users
Dear Experts,

I am transitioning from Matlab-based DMAT to python-based HDDM and really appreciate your help with a naive plotting problem.

I am using the commands below to plot the posteriors of my parameters (drift-rate in this case, please find attached the output image)

v_f, v_4, v_8, v_16, v_32, v_64, v_128  = m2.nodes_db.node[['v(frequent)', 'v(O4)', 'v(O8)', 'v(O16)', 'v(O32)', 'v(O64)', 'v(O128)']]
hddm.analyze.plot_posterior_nodes([v_f, v_4, v_8, v_16, v_32, v_64, v_128 ])

I would like to change color, line width, style, etc for each of the posterior images (i.e. v_f in dotted blue). Is there an easy way to do this?


Many thanks,
Mahsa
Screen Shot 2020-02-19 at 12.39.08 AM.png

Esin Turkakin

unread,
Feb 20, 2020, 9:29:48 AM2/20/20
to hddm-users
(Not an expert, but I'll share what I've found out through trial and error). I think it's actually quite similar to how MATLAB deals with plots -- once you run the plot_posterior_nodes(), you can modify the plot properties with subsequent lines and the changes will apply to the most recently generated plot. It definitely works if you run the whole chunk of lines relating to the same plot at the same time, it may or may not work if you go line by line. I'll append some code below as an example. This one cycles through two colors and three line types to plot a 2x3 interaction, but defining colors in a gradient for continuous variables is also possible with this method as matplotlib can also deal with rgb values (other ways to specify colors here). You can also change other stuff like axis labels, legend location (and many more not specified here) in a similar way. Note that the cycler is sticky and all subsequent plots will follow that scheme in that console until you change it.

#once you've extracted the nodes from the model

plt.rcParams['axes.prop_cycle'] = ("cycler('color', ['#0080ff','#0080ff','#0080ff','#fe420f','#fe420f','#fe420f']) +"
                                   "cycler('linestyle', ['-','--',':','-','--',':'])")
hddm.analyze.plot_posterior_nodes([cond_A_1, cond_A_2, cond_A_3, cond_B_1, cond_B_2, cond_B_3])
plt.xlabel('Starting Point')
plt.ylabel('Posterior probability')
plt.legend(['Cond A 1', 'Cond A 2', 'Cond A 3', 'Cond B 1', 'Cond B 2', 'Cond B 3'], 
           loc='center left', bbox_to_anchor=(1, 0.5))

Hope that helps at least towards more helpful google searches :)
Cheers,
Esin

Esin Turkakin

unread,
Feb 20, 2020, 9:48:16 AM2/20/20
to hddm-users
Just to avoid any confusion, I should note that in the code below, plt refers to matplotlib.pyplot (it seems that it's rather conventional to do that, but it's not a 'default' per se). So if your code doesn't have the following line somewhere, you will want to include that (with the other imports at the beginning or right before you start plotting):

import matplotlib.pyplot as plt

Cheers,
Esin

Mahsa Alizadeh Shalchy

unread,
Feb 24, 2020, 11:02:54 AM2/24/20
to hddm-...@googlegroups.com
Dear Esin,

Thank you so much for the detailed explanation, it was very helpful :)

For anyone who may look into this thread later: Another way to change the plotting properties is to use plt.gca() and indexing (see below)

plt.gca().get_lines()[0].set_color("Gray")
ax=plt.gca()
lines0= ax.get_lines()[0]
plt.setp(lines0, linestyle='--', linewidth=6)

Many thanks,
Mahsa


--
You received this message because you are subscribed to the Google Groups "hddm-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hddm-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/hddm-users/c7f02d86-cf2e-4d06-94e3-804621f762b1%40googlegroups.com.


--
***************************************
Mahsa Alizadeh Shalchy
Doctoral Researcher
Department of Psychology
University of California - Riverside
900 University Avenue 
Riverside California 92521
***************************************

Ramesh Karnani

unread,
Oct 6, 2021, 7:32:23 AM10/6/21
to hddm-users
Dear Esin,

Thanks for the above tip with respect to the axes.prop_cycle. With color and linestyle, I was able to plot "customized" figures. 

I did run across one issue and wanted to see if you or others have any solution.

(1) In addition to color and linestyle, I wanted to add line width to the cycler in the same manner 

plt.rcParams['axes.prop_cycle'] = ("cycler('color', ['#0080ff','#0080ff','#0080ff','#fe420f','#fe420f','#fe420f']) +"
                                   "cycler('linestyle', ['-','--',':','-','--',':']) +"
                                    "cycler('linewidth', ['1.0', '4.0', '1.0', '4.0', '1.0', '4.0']")

The code executes without error and I get a plot, however, the linewidths do not change. It is the same output as without the additional linewidth cycler. 

I also tried only using two cyclers - one for color and the other for line width, however, again, the linewidth did not change.

Would greatly appreciate any help with regard to this.

Thanks!
Ramesh
Reply all
Reply to author
Forward
0 new messages