有人能建议我如何更改代码以便: - 更多间隔的刻度值,以 0.5 为单位 - 将每个刻度标签格式化为 1 位小数
这就是我目前的情况:
\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\renewcommand{\thesection}{\arabic{section}}
\usepackage{mathtools}
\usepackage{cancel}
\usepackage{pgfplots}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{shapes.gates.logic.US,shapes.gates.logic.IEC,calc}
\usepackage{polynom}
\usepackage{flexisym}
\usepackage{wallpaper}
\usepackage{blkarray}
\usepackage{caption}
\usepgfplotslibrary{fillbetween}
\usepgfplotslibrary{statistics}
\newenvironment{tightcenter}{
\setlength\topsep{0pt}
\setlength\parskip{0pt}
\begin{center}}{\end{center}}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw[latex-] (-1,0) -- (2.5,0);
\draw[-latex] (-1,0) -- (2.5,0);
\foreach \x in {0,1,2}
\draw[shift={(\x,0)},color=black] (0pt,1pt) -- (0pt,-1pt);
\foreach \x in {0,0.5,1,1.5}
\draw[shift={(\x,0)},color=black] (0pt,0pt) -- (0pt,-1pt) node[below]
{$\x$};
%\node at (2.5,-1) {\text{Number of smart devices purchased}};
\draw[fill=black] (0,0.5) circle (2pt);
\draw[fill=black] (0,1.0) circle (2pt);
\draw[fill=black] (0,1.5) circle (2pt);
\draw[fill=black] (1,0.5) circle (2pt);
\draw[fill=black] (1,1.0) circle (2pt);
\draw[fill=black] (2,0.5) circle (2pt);
\draw[fill=black] (2,1.0) circle (2pt);
\end{tikzpicture}
\end{center}
\end{document}
太感谢了!
答案1
位数可以固定,例如说\pgfmathprintnumber[fixed,fixed zerofill,precision=1]{\x}
。要拉伸水平方向(不使圆圈变形),您可以使用x={(1.5,0)}
,例如。
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[x={(1.5,0)}]
\draw[latex-latex] (-1,0) -- (2.5,0);
\foreach \x in {0,1,2}
\draw[shift={(\x,0)},color=black] (0pt,1pt) -- (0pt,-1pt);
\foreach \x in {0,0.5,1,1.5}
\draw[shift={(\x,0)},color=black] (0pt,0pt) -- (0pt,-1pt) node[below]
{$\pgfmathprintnumber[fixed,fixed zerofill,precision=1]{\x}$};
\draw[fill=black] (0,0.5) circle (2pt);
\draw[fill=black] (0,1.0) circle (2pt);
\draw[fill=black] (0,1.5) circle (2pt);
\draw[fill=black] (1,0.5) circle (2pt);
\draw[fill=black] (1,1.0) circle (2pt);
\draw[fill=black] (2,0.5) circle (2pt);
\draw[fill=black] (2,1.0) circle (2pt);
\end{tikzpicture}
\end{document}
您加载了pgfplots
(以及许多与示例实际上无关的包),但没有使用它。pgfplots
您也可以通过绘图来绘制图表only marks,mark=*
,这样添加刻度就没那么麻烦了。