我目前得到的是:
\begin{tikzpicture}
\begin{axis}[
width=6cm,
height=6cm,
x axis line style={-stealth},
y axis line style={-stealth},
title={Signal 1},
xticklabels={},
ymax = 1.5,xmax=4,
axis lines*=center,
ytick={-1,1},yticklabels={$-\hat{i}$,$\hat{i}$},
xtick={1,2,3},xticklabels={$\frac{T}{3}$,$\frac{2T}{3}$,$T$},
%ticklabel style={at={(1,0)},xshift=1.5ex, anchor=north},
xlabel={$t$},
ylabel={$i(t)$},
every axis x label/.style={
at={(ticklabel* cs:1.05)},
anchor=west,
},
every axis y label/.style={
at={(ticklabel* cs:1.05)},
anchor=south,
},]
\addplot+[line width=0.5mm,mark=none,const plot]
coordinates
{(0,0) (0,1) (1,-1) (2,-1) (2,0) (3,0) (3,1) (4,1)};
\end{axis}
\end{tikzpicture}
据我所知,额外的刻度不起作用,因为所有标签都必须移动到不同的位置。
答案1
纯tikz
:
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}[
tick/.style = {inner sep=1pt, font=\scriptsize}
]
% axis
\draw[-Straight Barb] (-0.2,0) -- + (4,0) node[right] {$t$};
\foreach \x [count=\ix] in {\frac{T}{3}, \frac{2T}{3}, T}
\node[tick,below right] at (\ix,0) {$\x$};
\draw[-Straight Barb] (0,-1.2) -- + (0,2.4) node[above] {$i(t)$};
\draw (0,-1) -- + (-0.1,0) node[tick,left] {$-\hat{\imath}$};
\draw (0, 1) -- + (-0.1,0) node[tick,left] {$ \hat{\imath}$};
% function
\draw[line width=0.5mm, blue] plot coordinates
{(0,0) (0,1) (1,1) (1,-1) (2,-1) (2,0) (3,0) (3,1) (3.5,1)};
\end{tikzpicture}
\end{document}
答案2
您pgfplots
可以使用\hspace{}
水平移动 x 标签,并使用extra x tick style
和xtra x ticks
放置您想要应用不同样式的标签:
xtick={1,3},
xticklabels={\hspace*{2.0ex}$\frac{T}{3}$,$T$},
extra x tick style={x tick label style={above, yshift=0.6ex}},
extra x ticks={2},
得出的结果是:
代码:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{graphicx}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=6cm,
height=6cm,
x axis line style={-stealth},
y axis line style={-stealth},
title={Signal 1},
%xticklabels={},% <--- Of no use, since it is redefined later
ymax = 1.5,xmax=4,
axis lines*=center,
ytick={-1,1},
yticklabels={$-\hat{i}$,$\hat{i}$},
xtick={1,3},
xticklabels={\hspace*{2.0ex}$\frac{T}{3}$,$T$},
extra x tick style={x tick label style={above, yshift=0.6ex}},
extra x ticks={2},
%ticklabel style={at={(1,0)},xshift=1.5ex, anchor=north},
xlabel={$t$},
ylabel={$i(t)$},
every axis x label/.style={
at={(ticklabel* cs:1.05)},
anchor=west,
},
every axis y label/.style={
at={(ticklabel* cs:1.05)},
anchor=south,
},
]
\addplot+[line width=0.5mm,mark=none,const plot]
coordinates
{(0,0) (0,1) (1,-1) (2,-1) (2,0) (3,0) (3,1) (4,1)};
\end{axis}
\end{tikzpicture}%
\end{document}