我正在尝试绘制一个光锥图,并希望将时间轴和空间轴叠加在已有的图表顶部。我还想在每个锥体的两个对角线上添加方向箭头,但我知道轴的代码可以轻松重新用于实现这一点。
这是我目前的代码,但我意识到我对如何一起绘制多个形状的理解不足。每当我改变轴时,它都会改变底部锥体的定位,我不知道如何避免这种情况。这是我到目前为止一直使用的代码,任何见解都将不胜感激:
\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{70}{130}
\begin{center}
\begin{tikzpicture}
%Cone data
\pgfmathsetmacro{\radiush}{2};%Cone base radius
\pgfmathsetmacro{\theight}{-5}%Cone height (negative if you want a inverse cone)
\pgfmathsetmacro{\cheightp}{.4}%Cut height in percent of cone height
%Calculating coordinates
\coordinate (center) at (0,0);
\pgfmathsetmacro{\radiusv}{.2 * \radiush};
\pgfmathsetmacro{\sradiush}{\radiush * (1 - \cheightp)};%only for right circular cone
\pgfmathsetmacro{\sradiusv}{.2 * \sradiush};
\coordinate (peak) at ($(center) + (0,\theight)$);
\coordinate (vert1) at ($(center)+(\radiush,0)$);
\coordinate (vert2) at ($(center)-(\radiush,0)$);
\coordinate (svert1) at ($(vert1)!\cheightp!(peak)$);
\coordinate (svert2) at ($(vert2)!\cheightp!(peak)$);
%Drawing
\fill[left color=blue!30,middle color=blue!10, right color=blue!30,shading=axis,draw=black] (svert1) -- (peak) -- (svert2) arc (-180:360:\sradiush cm and \sradiusv cm);
%Lines, \h in percent of cone height
\foreach \h in {.5,.6,.7,.8}{
\pgfmathsetmacro{\rh}{\radiush * (1 - \h)}
\pgfmathsetmacro{\rv}{.2 * \rh}
\draw[black!70,densely dashed] ($(vert2)!\h!(peak)$) arc (180:360:\rh cm and \rv cm);
}
%\fill[inner color=gray!30,outer color=gray!50,shading=radial] (0,0) circle (\radiush cm and \radiusv cm);
\end{tikzpicture}
\begin{tikzpicture}
%Cone data
\pgfmathsetmacro{\radiush}{2};%Cone base radius
\pgfmathsetmacro{\theight}{5}%Cone height (negative if you want a inverse cone)
\pgfmathsetmacro{\cheightp}{.4}%Cut height in percent of cone height
%Calculating coordinates
\coordinate (center) at (0,0);
\pgfmathsetmacro{\radiusv}{.2 * \radiush};
\pgfmathsetmacro{\sradiush}{\radiush * (1 - \cheightp)};%only for right circular cone
\pgfmathsetmacro{\sradiusv}{.2 * \sradiush};
\coordinate (peak) at ($(center) + (0,\theight)$);
\coordinate (vert1) at ($(center)+(\radiush,0)$);
\coordinate (vert2) at ($(center)-(\radiush,0)$);
\coordinate (svert1) at ($(vert1)!\cheightp!(peak)$);
\coordinate (svert2) at ($(vert2)!\cheightp!(peak)$);
%Drawing
\fill[left color=red!70,right color=red!50,middle color=red!40,shading=axis,draw=black] (svert1) -- (peak) -- (svert2) arc (-180:360:\sradiush cm and \sradiusv cm);
%Lines, \h in percent of cone height
\foreach \h in {.5,.6,.7,.8}{
\pgfmathsetmacro{\rh}{\radiush * (1 - \h)}
\pgfmathsetmacro{\rv}{.2 * \rh}
\draw[black!70,densely dashed] ($(vert2)!\h!(peak)$) arc (180:360:\rh cm and \rv cm);
}
%\fill[inner color=gray!30,outer color=gray!50,shading=radial] (0,0) circle (\radiush cm and \radiusv cm);
\end{tikzpicture}
\end{center}
%Drawing the time axis (directed upwards)
\begin{center}
\begin{tikzpicture}
\draw[thick, ->] (15,0,0) -- (15,5,0) node[left] {$t$};
\end{tikzpicture}
\end{center}
\end{document}
答案1
基本上,我想你需要把所有东西都放在同一个位置tikzpicture
。我不知道你想如何将这些锥体相对于彼此定位,但你可以通过使用scope
带有yshift
和/或xshift
作为可选参数的环境(如下面的代码所示)或仅通过更改每个锥体的坐标位置来相对于彼此移动它们center
。在这种情况下,你不需要scope
s。
请注意,我改变了轴的坐标。
在底部添加标签的一种方法是
\node at ($(vert2)!\cheightp!(peak) + (0.25*\radiush cm,0)$) {A};
因为它使用其他宏和现有坐标,所以如果您更改这两个参数中的任何一个或锥体的中心,标签的位置将会更新。
\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{70}{130}
\begin{center}
\begin{tikzpicture}
\begin{scope}[yshift=1cm]
%Cone data
\pgfmathsetmacro{\radiush}{2};%Cone base radius
\pgfmathsetmacro{\theight}{-5}%Cone height (negative if you want a inverse cone)
\pgfmathsetmacro{\cheightp}{.4}%Cut height in percent of cone height
%Calculating coordinates
\coordinate (center) at (0,0);
\pgfmathsetmacro{\radiusv}{.2 * \radiush};
\pgfmathsetmacro{\sradiush}{\radiush * (1 - \cheightp)};%only for right circular cone
\pgfmathsetmacro{\sradiusv}{.2 * \sradiush};
\coordinate (peak) at ($(center) + (0,\theight)$);
\coordinate (vert1) at ($(center)+(\radiush,0)$);
\coordinate (vert2) at ($(center)-(\radiush,0)$);
\coordinate (svert1) at ($(vert1)!\cheightp!(peak)$);
\coordinate (svert2) at ($(vert2)!\cheightp!(peak)$);
%Drawing
\fill[left color=blue!30,middle color=blue!10, right color=blue!30,shading=axis,draw=black] (svert1) -- (peak) -- (svert2) arc (-180:360:\sradiush cm and \sradiusv cm);
%Lines, \h in percent of cone height
\foreach \h in {.5,.6,.7,.8}{
\pgfmathsetmacro{\rh}{\radiush * (1 - \h)}
\pgfmathsetmacro{\rv}{.2 * \rh}
\draw[black!70,densely dashed] ($(vert2)!\h!(peak)$) arc (180:360:\rh cm and \rv cm);
}
\node at ($(vert2)!\cheightp!(peak) + (0.25*\radiush cm,0)$) {A};
\end{scope}
%\fill[inner color=gray!30,outer color=gray!50,shading=radial] (0,0) circle (\radiush cm and \radiusv cm);
\begin{scope}[yshift=-2cm]
%Cone data
\pgfmathsetmacro{\radiush}{2};%Cone base radius
\pgfmathsetmacro{\theight}{5}%Cone height (negative if you want a inverse cone)
\pgfmathsetmacro{\cheightp}{.4}%Cut height in percent of cone height
%Calculating coordinates
\coordinate (center) at (0,0);
\pgfmathsetmacro{\radiusv}{.2 * \radiush};
\pgfmathsetmacro{\sradiush}{\radiush * (1 - \cheightp)};%only for right circular cone
\pgfmathsetmacro{\sradiusv}{.2 * \sradiush};
\coordinate (peak) at ($(center) + (0,\theight)$);
\coordinate (vert1) at ($(center)+(\radiush,0)$);
\coordinate (vert2) at ($(center)-(\radiush,0)$);
\coordinate (svert1) at ($(vert1)!\cheightp!(peak)$);
\coordinate (svert2) at ($(vert2)!\cheightp!(peak)$);
%Drawing
\fill[left color=red!70,right color=red!50,middle color=red!40,shading=axis,draw=black] (svert1) -- (peak) -- (svert2) arc (-180:360:\sradiush cm and \sradiusv cm);
%Lines, \h in percent of cone height
\foreach \h in {.5,.6,.7,.8}{
\pgfmathsetmacro{\rh}{\radiush * (1 - \h)}
\pgfmathsetmacro{\rv}{.2 * \rh}
\draw[black!70,densely dashed] ($(vert2)!\h!(peak)$) arc (180:360:\rh cm and \rv cm);
}
\node at ($(vert2)!\cheightp!(peak) + (0.25*\radiush cm,0)$) {B};
%\fill[inner color=gray!30,outer color=gray!50,shading=radial] (0,0) circle (\radiush cm and \radiusv cm);
\end{scope}
\draw[thick, ->] (0,-5,0) -- (0,5,0) node[left] {$t$};
\end{tikzpicture}
\end{center}
\end{document}