这是我第一次使用 pgf 时的后续问题。现在图表已经达到了我想要的效果,但看起来还不够完美。我试着用手册弄清楚,但是……
- 是否可以在过长的标签中添加换行符(可能是自动的)?
- 可以格式化文本吗?当我使用 textbf 时,出现了错误
- 如何更改每个条形的颜色。我发现的唯一解决方案是删除文本标签
谢谢所有 tikz 专家提前致谢 :)
\documentclass[border=10pt]{standalone}
\usepackage{verbatim}
\usepackage{pgfplots}
\pgfplotsset{width=7cm, compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
width=0.7\textwidth,
height=0.7\textwidth,
bar width=.5\textwidth,
point meta=explicit symbolic,
nodes near coords,
enlargelimits=0,
ylabel={Spannung in V},
symbolic x coords={tool1},
xtick=data,
xtick style={draw=none},
xticklabel=\empty,
ymin=0,
ymax=70,
]
\addplot+[ybar] plot coordinates {(tool1,20) [thisisalongword]};
\addplot+[ybar] plot coordinates {(tool1,4) [thisisalongword]};
\addplot+[ybar] plot coordinates {(tool1,12) [thisisalongword with anotherthisisalongword]};
\addplot+[ybar] plot coordinates {(tool1,16) [thisisalongword with anotherthisisalongword]};
\addplot+[ybar] plot coordinates {(tool1,2) [thisisalongword with anotherthisisalongword]};
\addplot+[ybar] plot coordinates {(tool1,4) [andagain thisisalongerword]};
\addplot+[ybar] plot coordinates {(tool1,2) [thisisalongword]};
\addplot+[ybar] plot coordinates {(tool1,10) [andagain thisisalongerword]};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
- 使用
every node near coord/.style={text width=3 cm,},
您可以设置文本节点的宽度。 - 添加
fill=color
如\addplot
:\addplot+[ybar, fill=red] plot....
改变颜色。 - 您可以通过以下方式使字体加粗
every node near coord/.style={font=\bfseries},
您还可以通过 进行更多更改every node near coord/.style
。例如,如果您不喜欢文本左对齐,但希望其居中,则添加align=center
。
平均能量损失
\documentclass[border=10pt]{standalone}
\usepackage{verbatim}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
width=0.7\textwidth,
height=0.7\textwidth,
bar width=.5\textwidth,
point meta=explicit symbolic,
nodes near coords,
enlargelimits=0,
ylabel={Spannung in V},
symbolic x coords={tool1},
xtick=data,
xtick style={draw=none},
xticklabel=\empty,
ymin=0,
ymax=70,
every node near coord/.style={text width=3 cm, font=\bfseries, align=center},
]
\addplot+[ybar, fill=red] plot coordinates {(tool1,20) [thisisalongword]};
\addplot+[ybar, fill=blue] plot coordinates {(tool1,4) [thisisalongword]};
\addplot+[ybar] plot coordinates {(tool1,12) [thisisalongword with anotherthisisalongword]};
\addplot+[ybar] plot coordinates {(tool1,16) [thisisalongword with anotherthisisalongword]};
\addplot+[ybar] plot coordinates {(tool1,2) [thisisalongword with anotherthisisalongword]};
\addplot+[ybar] plot coordinates {(tool1,4) [andagain thisisalongerword]};
\addplot+[ybar] plot coordinates {(tool1,2) [thisisalongword]};
\addplot+[ybar] plot coordinates {(tool1,10) [andagain thisisalongerword]};
\end{axis}
\end{tikzpicture}
\end{document}