3

I'm a student majoring in mechanical engineering and I'm working with plotting the deformation of two links connected at an certain angle. The schematic diagram of the two links is shown below

enter image description here

I want to use the package pgfplots to plot the deformation of the links. Now I have calculated the deformation of the two links at some nodes. Suppose the deformations of the two links both are

\begin{filecontents*}{data1.tex}
0.  0.
0.1 0.00944826
0.2 0.0308185
0.3 0.0541806
0.4 0.0711639
0.5 0.0760562
0.6 0.0666065
0.7 0.0442508
0.8 0.0136516
0.9 -0.0183828
1.  -0.0443726
\end{filecontents*}

Where the first column is the distance along the axial direction of the link and the second column is the transverse deformation. To intuitively show the deformations, I want to use the mesh option of the \addplot command to color the line according to the deformation by setting point meta=y. In addition, I want to make the deformation plotted with respect to the undeformed configuration, as shown below.

enter image description here

The code I write is

\documentclass{article}
\usepackage{pgfplots,pgfplotstable}
\usepackage{filecontents}
\begin{filecontents*}{data.dat}
        0.  0.
        0.1 0.00944826
        0.2 0.0308185
        0.3 0.0541806
        0.4 0.0711639
        0.5 0.0760562
        0.6 0.0666065
        0.7 0.0442508
        0.8 0.0136516
        0.9 -0.0183828
        1.  -0.0443726
    \end{filecontents*}
\pagestyle{empty}
\pgfplotsset{compat=1.16}
\begin{document}
\pgfplotstableread{data.dat}\data
    \begin{tikzpicture}
        \begin{axis}[
            % hide axis,
            colormap/jet,
            point meta rel=per plot,
            ymax=10
        ]
            \addplot[
                mesh,
                point meta=abs(y),
            ] table {\data} coordinate (endpoint);
            % the above command plots the deformation of the first link
            \addplot[
                mesh,
                x filter/.code={\pgfmathparse{#1+1}\pgfmathresult},
                y filter/.code={\pgfmathparse{#1+-0.0443726}\pgfmathresult},
                rotate around={45:(1.,-0.0443726)},% works
                %rotate around={45:(endpoint)},% not works
                ] table {\data};
        \end{axis}
    \end{tikzpicture}

\end{document}

In the above code, I use x filter and rotate around to achieve the rotation and shift of the plot. I need to manually modify the rotation parameter, which seems to be cumbersome if the deformation is changed. I need to plot many similar figures.

I define a coordinate named endpoint in the end of the first addplot. If I use the coordinate name as the variable in rotate around(see commented line), the code doesn't work.

How can I modify the code so that I can only change the file name?

1 Answer 1

2

Nice question! Your approach works in principle, but the problem is that pgfplots launches a survey phase during which the endpoint coordinate is not known. Therefore I suggest to just read the last coordinate from the table (before you start the axis) and use it. This is as automatic as your approach.

\documentclass{article}
\usepackage{pgfplots,pgfplotstable}
\usepackage{filecontents}
\begin{filecontents*}{data.dat}
        0.  0.
        0.1 0.00944826
        0.2 0.0308185
        0.3 0.0541806
        0.4 0.0711639
        0.5 0.0760562
        0.6 0.0666065
        0.7 0.0442508
        0.8 0.0136516
        0.9 -0.0183828
        1.  -0.0443726
    \end{filecontents*}
\pagestyle{empty}
\pgfplotsset{compat=1.16}
\begin{document}
\pgfplotstableread{data.dat}\data
\pgfplotstablegetrowsof{\data} 
\pgfmathtruncatemacro{\NumRows}{\pgfplotsretval-1} 
\pgfplotstablegetelem{\NumRows}{[index]0}\of{\data}
\edef\myx{\pgfplotsretval}
\pgfplotstablegetelem{\NumRows}{[index]1}\of{\data}
\edef\myy{\pgfplotsretval}
    \begin{tikzpicture}
        \begin{axis}[
             hide axis,
            colormap/jet,
            point meta rel=per plot,
            ymax=10
        ]
            \addplot[
                mesh,
                point meta=abs(y),
            ] table {\data};
            \addplot[
                mesh,
                x filter/.code={\pgfmathparse{#1+1}\pgfmathresult},
                y filter/.code={\pgfmathparse{#1+-0.0443726}\pgfmathresult},
                rotate around={45:(\myx,\myy)},% works
                ] table {\data};
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .