Graph plot: vertex attributes, node size, arrow head for loops, why are loops on nodes

asked 2020-12-17 19:43:31 +0100

Cyrille gravatar image

updated 2020-12-19 14:45:39 +0100

slelievre gravatar image

Sorry, some of you think that I do not read the docs but the truth is that even reading them I cannot find my way.

Before using SageMath I was mainly working with PSTricks with which I have no problem drawing what I expect. If only it could be incorporated completely it would be a great moment.

Here is my problem: the code below works but I do not understand how to set the vertex attributes, the size of nodes, and so on.

A = matrix([
    [1,1,0,1,0,0,0,0,0],
    [1,1,1,0,1,0,0,0,0],
    [0,1,1,0,0,1,0,0,0],
    [1,0,0,1,1,0,1,0,0],
    [0,1,0,1,1,1,0,1,0],
    [0,0,1,0,1,1,0,0,1],
    [0,0,0,1,0,0,1,1,0],
    [0,0,0,0,1,0,1,1,1],
    [0,0,0,0,0,1,0,1,1]])

D = DiGraph(A, format='adjacency_matrix')
D.show(figsize=16, vertex_size=100)

Grid graph with unsatisfactory loops

But I do not understand how to set the vertex attributes, the size of the nodes, and so on. I even wonder why loops have no arrow head, why loops are plotted on top of nodes rather than underneath them. This is for my very strange since I have understood how to change the colors of the arcs. I truly need help (sorry to waste your time). I would also like to know if one can use images as vertex attributes.

vertex_attrs={'label':['1','2','3','4','5','6','7','8','9',]}

Added to answer @slelievre's comments

First of all my above code is only 3 lines. I have made many tries to insert vertex_attributes. None works.

Here is what I am able to achieve with PSTricks:

Graph produced with PSTricks

I can provide the code. As I told you (this is also an answer to the comment to @dazedANDconfused), I work now mainly in html with SageCell because it's mandatory in these lockdown times. Most of my students (in economics) are poorly endowed in computers (some work with tablets or even phones). With html embedded SageCells I can overcome all those difficulties since computation are not realized in situ.

I was delighted in discovering Latex2js since their exemples are incredible. But all my trials are poor. It is not complete and their sandbox doesn't work (at least for me). But I think also that as SageMath is a pure killing software, it must be raised to that level.

Science is a pure public good. We are paid by government to increase common knowledge. But it is difficult to share it if you must pay for being published or to acquire software.

PSTricks code and node images as requested by slelievre

Note: my LaTeX preamble uses lots of PStricks packages and I do not remember which are used for this particular graph (I use also *graphix).

\begin{figure}[H]
\begin{center}
\begin{pspicture}(-3.2,-3.6)(3.6,3.6)
%\psgrid
%\pscircle(0,0){3cm}
\SpecialCoor
\rput(3;0){\rnode{A}{\includegraphics[scale=.25]{Avat0.png}$_a$}}
\rput(3;72){\rnode{B}{\includegraphics[scale=.25]{Avat1.png}$_b$}}
\rput(3;142){\rnode{C}{\includegraphics[scale=.25]{Avat2.png}$_c$}}
\rput(3;214){\rnode{D}{\includegraphics[scale=.25]{Avat5.png}$_d$}}
\rput(3;286){\rnode{E}{\includegraphics[scale=.25]{Avat4.png}$_e$}}
\ncarc[nodesep=3pt,linecolor=venetianred,linewidth=1.2pt]{->}{B}{A}
\ncarc[nodesep=3pt,linecolor=venetianred,linewidth=1.2pt]{<-}{C}{A}
\ncarc[nodesep=3pt,linecolor=venetianred,linewidth=1.2pt]{<-}{D}{A}
\ncarc[nodesep=3pt,linecolor=venetianred,linewidth=1.2pt]{<-}{A}{E}
\ncarc[nodesep=3pt,linecolor=venetianred,linewidth=1.2pt]{<-}{C}{B}
\ncarc[nodesep=3pt,linecolor=venetianred,linewidth=1.2pt]{<-}{D}{B}
\ncarc[nodesep=3pt,linecolor=venetianred,linewidth=1.2pt]{->}{B}{E}
\ncarc[nodesep=3pt,linecolor=venetianred,linewidth=1.2pt]{<-}{D}{C}
\ncarc[nodesep=3pt,linecolor=venetianred,linewidth=1.2pt]{<-}{E}{D}
\ncarc[nodesep=3pt,linecolor=venetianred,linewidth=1.2pt]{<-}{C}{E}
\NormalCoor
\cnodeput[linestyle=none,fillstyle=solid, fillcolor=white, shadow=true,blur=true](2.2,1.4){AA}{\tiny2/\textcolor{venetianred}3}
\cnodeput[linestyle=none,fillstyle=solid, fillcolor=white, shadow=true,blur=true](0.4,1.2){BB}{\tiny\textcolor{venetianred}{3}/2}
\cnodeput[linestyle=none,fillstyle=solid, fillcolor=white, shadow=true,blur=true](-1,-1){CC}{\tiny\textcolor{venetianred}{3}/2}
\cnodeput[linestyle=none,fillstyle=solid, fillcolor=white, shadow=true,blur=true](2,-1.6){DD}{\tiny2/\textcolor{venetianred}{3}}
\cnodeput[linestyle=none,fillstyle=solid, fillcolor=white, shadow=true,blur=true](-.5,2.6){EE}{\tiny\textcolor{venetianred}{4}/1}
\cnodeput[linestyle=none,fillstyle=solid, fillcolor=white, shadow=true,blur=true](-1.6,-.2){FF}{\tiny\textcolor{venetianred}{4}/1}
\cnodeput[linestyle=none,fillstyle=solid, fillcolor=white, shadow=true,blur=true](1.,-1.4){GG}{\tiny\textcolor{venetianred}{3}/2}
\cnodeput[linestyle=none,fillstyle=solid, fillcolor=white, shadow=true,blur=true](-2.6,0){HH}{\tiny\textcolor{venetianred}{3}/2}
\cnodeput[linestyle=none,fillstyle=solid, fillcolor=white, shadow=true,blur=true](.2,-1.6){II}{\tiny1/\textcolor{venetianred}{4}}
\cnodeput[linestyle=none,fillstyle=solid, fillcolor=white, shadow=true,blur=true](-1,-2.4){KK}{\tiny2/\textcolor{venetianred}{3}}
\end{pspicture}
\end{center}
\caption{\textsc{Scores dans les confrontations deux à deux}}\label{condorcetcomp}
\end{figure}

Node images: a · b · c · d · e.

Good luck working on that but I think it will be simpler to extend the show command to use PSTricks. That would make SageMath a pure wonder!

edit retag flag offensive close merge delete

Comments

Can you share an example (maybe prepared with PSTricks) of what you would like to achieve?

slelievre gravatar imageslelievre ( 2020-12-17 23:28:35 +0100 )edit

Thank you for asking so many questions.

Thanks for often providing some code to get answerers something to start from.

It's not clear how you are using vertex_attrs above.

Regarding vertex size, try using vertex_size=400 or vertex_size=1600. Notice the difference?

slelievre gravatar imageslelievre ( 2020-12-17 23:53:41 +0100 )edit

If you're interested in producing professional looking graphs then LaTeX is the way to go using the packages tkz-graph and tkz-berge. These can work with Sage via the sagetex package. You can see some examples here or here or, for digraphs here. In these cases, the edges vertex sizes, labels, etc are set through LaTeX. The cooperation between Sage and LaTex is not "incorporated completely" but does a decent job.

dazedANDconfused gravatar imagedazedANDconfused ( 2020-12-18 05:06:01 +0100 )edit

dazedANDconfused it's a good suggestion. My problem is that i have decided to work mainly with html including sagecells because it's a trully wonderfull machinery. Many of my students haven't the capacity of a good computation because they are working on tablets even on phone. And we need this because right now in my university we are teaching on internet. With html embedded SageMAthCells this difficulty is overcomed (to the limit of what they use to navigate on internet). But until now there are some limitation I would appreciate to be adapted.

Cyrille gravatar imageCyrille ( 2020-12-18 07:22:26 +0100 )edit

@Cyrille -- thanks for providing an example of desired output. Ideally, please also provide the PSTricks code, as well as the individual images for the nodes.

slelievre gravatar imageslelievre ( 2020-12-18 10:50:36 +0100 )edit