我使用一长串绘图坐标 (x,y) 生成了一个绘图。我想将数据绘制为 (y,x),其中 y 位于横轴,x 位于纵轴。有什么方法可以做到这一点吗?
我想避免再次生成数据或手动将 (x,y) 反转为 (y,x)。谢谢!
\begin{tikzpicture}
\begin{axis}[
width=2in,
height=2in,
scale only axis,
xmin=-0.2, xmax=0.15,
xtick={-0.2,-0.1,0,0.1},
scaled x ticks = false,
x tick label style={/pgf/number format/fixed},
ymin=-0.1, ymax=0.7,
scaled y ticks = false,
y tick label style={/pgf/number format/fixed},
axis lines=left,
axis on top]
\addplot[only marks,mark=*,color=blue,line width=2.0pt] plot coordinates{ (-0.157508722022685,-0.0618570189718741) (-0.151664833094664,-0.0515966883381529) (-0.145564835252092,-0.040823938776308) (-0.139190452250407,-0.0294967207779095) (-0.132521672953995,-0.0175681338079243) (-0.12553655513375,-0.00498567308655944) (-0.118211006929595,0.00830966963892551) (-0.110518545011038,0.0223845008246773) (-0.102430029660386,0.0373143588089115) (-0.0939133791975919,0.0531853589452309) (-0.0849332700533765,0.0700962421608876) (-0.0754508355568791,0.0881609593871301) (-0.0654235205799195,0.107511971554234) (-0.0548043511797005,0.128304532815312) (-0.0435426432953672,0.150722344204677) (-0.0315837272782253,0.174985172251473) (-0.0188698246180387,0.201359353223726) (-0.00534192152608734,0.230172683799602) (0.00905671632198194,0.261836217436385) (0.0243733112413743,0.296877414534431) (0.0406316912795995,0.335992918393738) (0.0578048302999246,0.380137343750061) (0.0757586843741441,0.430683077324108) (0.0941307367012631,0.489733069616693) (0.112044793360859,0.560802333252538) (0.127355632830606,0.65053016141635) };
\end{axis}
\end{tikzpicture}
答案1
正如 Jake 所提到的,使用表格进行此类操作要容易得多。这里有一个快速技巧,可以忽略(
,)
字符并将列分隔符设为逗号,这样我们就可以使用 coords 作为内联表格条目。(我还交换了 x 和 y 设置)
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=2in,
height=2in,
scale only axis,
ymin=-0.2, ymax=0.15,
ytick={-0.2,-0.1,0,0.1},
scaled x ticks = false,
x tick label style={/pgf/number format/fixed},
xmin=-0.1, xmax=0.7,
scaled y ticks = false,
y tick label style={/pgf/number format/fixed},
axis lines=left,
axis on top]
\addplot[only marks,mark=*,color=blue,line width=2.0pt] plot table[ignore chars={(,)},
col sep=comma,
x index=1,
y index=0]
{ (-0.157508722022685,-0.0618570189718741)
(-0.151664833094664,-0.0515966883381529)
(-0.145564835252092,-0.040823938776308)
(-0.139190452250407,-0.0294967207779095)
(-0.132521672953995,-0.0175681338079243)
(-0.12553655513375,-0.00498567308655944)
(-0.118211006929595,0.00830966963892551)
(-0.110518545011038,0.0223845008246773)
(-0.102430029660386,0.0373143588089115)
(-0.0939133791975919,0.0531853589452309)
(-0.0849332700533765,0.0700962421608876)
(-0.0754508355568791,0.0881609593871301)
(-0.0654235205799195,0.107511971554234)
(-0.0548043511797005,0.128304532815312)
(-0.0435426432953672,0.150722344204677)
(-0.0315837272782253,0.174985172251473)
(-0.0188698246180387,0.201359353223726)
(-0.00534192152608734,0.230172683799602)
(0.00905671632198194,0.261836217436385)
(0.0243733112413743,0.296877414534431)
(0.0406316912795995,0.335992918393738)
(0.0578048302999246,0.380137343750061)
(0.0757586843741441,0.430683077324108)
(0.0941307367012631,0.489733069616693)
(0.112044793360859,0.560802333252538)
(0.127355632830606,0.65053016141635)
};
\end{axis}
\end{tikzpicture}
\end{document}