我使用 Python 绘制了以下图表matplotlib
:
我想使用 TikZ 改进它并添加一些标签。我遇到的问题是标签彼此太近,最终全部重叠。
有什么方法可以让这个图更清晰吗?如果蓝色标签消失,我可以接受,但其他标签非常重要。我认为应该旋转图并以某种方式移动标签,但不幸的是我不知道该怎么做。
编辑:如果这有助于使情节更清晰,可以改变点的位置(即,\Px
如果点位于球体表面上,\Py
则可以将和修改为您想要的任何位置)。\Pz
这是我的代码:
\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{calc}
\usepackage{tikz-3dplot}
\tdplotsetmaincoords{60}{115}
\pgfplotsset{compat=newest}
\newcommand{\comment}[1]{}
\newcommand{\pvec}[1]{\vec{#1}\mkern2mu\vphantom{#1}}
\begin{document}
\def\Px{1/sqrt(33)}
\def\Py{4/sqrt(33)}
\def\Pz{4/sqrt(33)}
\def\Ef{1.4}
\def\Er{0.33}
\def\Qz{1.4}
\begin{tikzpicture}[tdplot_main_coords, scale = 2.5]
\coordinate (O) at (0, 0, 0);
\coordinate (P1) at ({\Px},{\Py},{\Pz});
\coordinate (dE1) at ({\Px-\Px*\Ef},{\Py-\Py*\Ef},{\Pz+(\Qz-\Pz)*\Ef});
\coordinate (P2) at ({\Py},{\Px},{\Pz});
\coordinate (dE2) at ({\Py-\Py*\Ef},{\Px-\Px*\Ef},{\Pz+(\Qz-\Pz)*\Ef});
\coordinate (P3) at ({-\Px},{-\Py},{\Pz});
\coordinate (dE3) at ({-\Px+\Px*\Ef},{-\Py+\Py*\Ef},{\Pz+(\Qz-\Pz)*\Ef});
\coordinate (P4) at ({-\Py},{-\Px},{\Pz});
\coordinate (dE4) at ({-\Py+\Py*\Ef},{-\Px+\Px*\Ef},{\Pz+(\Qz-\Pz)*\Ef});
\coordinate (Q) at (0, 0, \Qz);
\def\arrayP{(P1), (P2), (P3), (P4)}
\def\arrayE{(dE1), (dE2), (dE3), (dE4)}
% Sphere
\shade[ball color = lightgray, opacity = 0.5] (0,0,0) circle (1cm);
% Sphere's dotted lines
\tdplotsetrotatedcoords{0}{0}{0};
\draw[dashed, tdplot_rotated_coords, gray] (0,0,0) circle (1);
\tdplotsetrotatedcoords{90}{90}{90};
\draw[dashed, tdplot_rotated_coords, gray] (1,0,0) arc (0:360:1);
\tdplotsetrotatedcoords{0}{90}{90};
\draw[dashed, tdplot_rotated_coords, gray] (1,0,0) arc (0:360:1);
\draw[dashed, gray] (O) -- (-1,0,0);
\draw[dashed, gray] (O) -- (0,-1,0);
\draw[dashed, gray] (O) -- (0,0,-1);
% Axes
\draw[-stealth] (O) -- (1.80,0,0) node[below left] {$x$};
\draw[-stealth] (O) -- (0,1.30,0) node[below right] {$y$};
\draw[-stealth] (O) -- (0,0,2.3) node[above] {$z$};
% Charges
\foreach \p [count=\i] in \arrayP {
\draw[thick, -stealth] (O) -- \p node[right] {$\pvec{r}'_{\i}$};
\draw[thick, blue, -stealth] \p -- (Q) node[right] {$\vec{r}-\pvec{r}'_{\i}$};
}
% Differential fields
\foreach \p [count=\i] in \arrayE {
\draw[thick, red, -stealth] (Q) -- \p node[left] {$d\vec{E}_{\i}$};
}
% Total field
\draw[thick, teal, -stealth] (Q) -- ($\Er*(dE1) + \Er*(dE2) + \Er*(dE3) + \Er*(dE4)$) node[right] {$\vec{E}$};
\end{tikzpicture}
\end{document}
答案1
我没有完美的解决方案,但有一些建议。你可能需要
- 稍微增加一点规模。;
- 在背景层上绘制球体内部的矢量;
- 改变节点锚点;
- 使用适当的差异
d
。
结果:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{backgrounds}
\usepackage{tikz-3dplot}
\tdplotsetmaincoords{60}{115}
\newcommand{\pvec}[1]{\vec{#1}\mkern2mu\vphantom{#1}}
\begin{document}
\def\Px{1/sqrt(33)}
\def\Py{4/sqrt(33)}
\def\Pz{4/sqrt(33)}
\def\Ef{1.4}
\def\Er{0.33}
\def\Qz{1.4}
\begin{tikzpicture}[tdplot_main_coords, scale = pi]
\coordinate (O) at (0, 0, 0);
\coordinate (P1) at ({\Px},{\Py},{\Pz});
\coordinate (dE1) at ({\Px-\Px*\Ef},{\Py-\Py*\Ef},{\Pz+(\Qz-\Pz)*\Ef});
\coordinate (P2) at ({\Py},{\Px},{\Pz});
\coordinate (dE2) at ({\Py-\Py*\Ef},{\Px-\Px*\Ef},{\Pz+(\Qz-\Pz)*\Ef});
\coordinate (P3) at ({-\Px},{-\Py},{\Pz});
\coordinate (dE3) at ({-\Px+\Px*\Ef},{-\Py+\Py*\Ef},{\Pz+(\Qz-\Pz)*\Ef});
\coordinate (P4) at ({-\Py},{-\Px},{\Pz});
\coordinate (dE4) at ({-\Py+\Py*\Ef},{-\Px+\Px*\Ef},{\Pz+(\Qz-\Pz)*\Ef});
\coordinate (Q) at (0, 0, \Qz);
\def\arrayP{(P1), (P2), (P3), (P4)}
\def\arrayE{(dE1), (dE2), (dE3), (dE4)}
% Sphere
\shade[ball color = lightgray, opacity = 0.8] (0,0,0) circle[radius=1cm];
% Sphere's dotted lines
\tdplotsetrotatedcoords{0}{0}{0};
\draw[dashed, tdplot_rotated_coords, gray] (0,0,0) circle[radius=1];
\tdplotsetrotatedcoords{90}{90}{90};
\draw[dashed, tdplot_rotated_coords, gray] (1,0,0) arc (0:360:1);
\tdplotsetrotatedcoords{0}{90}{90};
\draw[dashed, tdplot_rotated_coords, gray] (1,0,0) arc (0:360:1);
\draw[dashed, gray] (O) -- (-1,0,0);
\draw[dashed, gray] (O) -- (0,-1,0);
\draw[dashed, gray] (O) -- (0,0,-1);
% Axes
\draw[-stealth] (O) -- (1.80,0,0) node[below left] {$x$};
\draw[-stealth] (O) -- (0,1.30,0) node[below right] {$y$};
\draw[-stealth] (O) -- (0,0,2.3) node[above] {$z$};
% Charges
\begin{scope}[on background layer]
\foreach \p [count=\i] in \arrayP {
\draw[thick, -stealth] (O) -- \p;
\ifnum\i=4
\draw[thick, blue, -stealth] \p -- (Q) node[pos=0.8,right=1ex] {$\vec{r}-\pvec{r}'_{\i}$};
\fi
%
}
\end{scope}
\foreach \p [count=\i] in \arrayP {
\path \p node[anchor=270-90*\i] {$\pvec{r}'_{\i}$};
\ifnum\i=1
\draw[thick, blue, -stealth] \p -- (Q) node[pos=0.6,right=1ex] {$\vec{r}-\pvec{r}'_{\i}$};
\else
\ifnum\i<4
\draw[thick, blue, -stealth] \p -- (Q) node[pos=0.6,left=1ex] {$\vec{r}-\pvec{r}'_{\i}$};
\fi
\fi
}
% Differential fields
\foreach \p [count=\i] in \arrayE {
\draw[thick, red, -stealth] (Q) -- \p
node[pos=1,anchor={-135+(\i<3 ?90*mod(\i,2):135*mod(\i+1,2))}] {$\mathrm{d}\vec{E}_{\i}$};
}
% Total field
\draw[thick, teal, -stealth] (Q) -- ($\Er*(dE1) + \Er*(dE2) + \Er*(dE3) + \Er*(dE4)$) node[right] {$\vec{E}$};
\end{tikzpicture}
\end{document}