个性化阅读
专注于IT技术分析

使用Tikz的Latex条形图和饼图

本文概述

条形图

条形图是用矩形条形表示的信息的图或图表, 这些矩形条的高度或宽度与它们所呈现的数据值相对应。条形图通常有两种类型, 垂直条形图和水平条形图。它显示了离散类别之间的比较。一些图形还显示了群集条, 这些群集条被分组并演示了多个测量变量的值。

Tikz是用于在Latex中创建图形元素的最强大, 最复杂的工具。在使用tikz的Latex中, 可以有效地创建条形图。

让我们考虑一个创建条形图的简单示例。

下面给出了该示例的代码:

\documentclass[12pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=6.6cm, compat=1.7}
\begin{document}
\begin{tikzpicture}

\begin{axis}
[
    ybar, enlargelimits=0.15, ylabel={\#Average Marks}, % the ylabel must precede a # symbol.
    xlabel={\ Students Name}, symbolic x coords={Tom, Jack, Hary, Liza, Henry}, % these are the specification of coordinates on the x-axis.
    xtick=data, nodes near coords, % this command is used to mention the y-axis points on the top of the particular bar.
    nodes near coords align={vertical}, ]
\addplot coordinates {(Tom, 50) (Jack, 90) (Hary, 70) (Liza, 80) (Henry, 60) };

\end{axis}
\end{tikzpicture}
\end{document}

输出:

使用Tikz的Latex条形图和饼图

让我们考虑另一个例子。它比第一个复杂, 但易于理解。

下面给出了该示例的代码:

\documentclass[12pt]{standalone} % the standalone environment is used to display the pictures.
\usepackage{pgfplots} % package used to implement the plot
\pgfplotsset{width=6.5cm, compat=1.6}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[
    ybar, % ybar command displays the graph in horizontal form, while the xbar command displays the graph in vertical form.
    enlargelimits=0.15, % these limits are used to shrink or expand the graph. The lesser the limit, the higher the graph will expand or grow. The greater the limit, the more graph will shrink. 
    legend style={at={(0.4, -0.25)}, % these are the measures of the bottom row containing surplus (wheat, Tea, rice), where -0.25 is the gap between the bottom row and the graph. 
      anchor=north, legend columns=-1}, % here, north is the position of the bottom legend row. You can specify the east, west, or south direction to shift the location. 
    ylabel={\#Annual Growth Percentage}, % there should be no line gap between the rows here. Otherwise, latex will show an error.
    symbolic x coords={2016, 2017, 2018}, xtick=data, nodes near coords, nodes near coords align={vertical}, ]
\addplot coordinates {(2016, 75) (2017, 78) (2018, 80)}; % these are the measures of a particular bar graph. The tick marks of the y-axis will be adjusted automatically according to the data values entered in the coordinates.
\addplot coordinates {(2016, 70) (2017, 63) (2018, 68)};
\addplot coordinates {(2016, 61) (2017, 55) (2018, 59)};
\legend{Wheat, Tea, Rice}

\end{axis}
\end{tikzpicture}
\end{document}

输出:

使用Tikz的Latex条形图和饼图

饼状图

饼图是统计图, 其形状为圆形。该图被进一步划分为定义数值比例的切片。

饼图用于表示整个关系的一部分。图表的所有部分总计为100%。饼图中每个类别中的棋子与整个类别的分数成比例。

下面给出了创建简单饼图的代码:

\documentclass[12pt]{article}
\usepackage{pgf-pie} % the package used to implement the pie charts
\begin{document}
\begin{tikzpicture} % Tikz environment

\pie
 {46/Rice, 32/Wheat, 22/Other} % it is essential to recheck that the sum of all the components mentioned should be 100%. Otherwise, Latex will leave the left percentage, blank.
     
\end{tikzpicture}
\end{document}

输出:

使用Tikz的Latex条形图和饼图

如果要旋转图表任何角度, 则需要使用rotate命令。

此类示例的代码如下:

\documentclass[12pt]{article}
\usepackage{pgf-pie} 
\begin{document}
\begin{tikzpicture} 

\pie[rotate=180]
 {46/Rice, 32/Wheat, 22/Other}
     
\end{tikzpicture}
\end{document}

输出:

使用Tikz的Latex条形图和饼图

如果要将文本放置在饼图中, 则需要使用text = inside命令。代码如下:

\documentclass[12pt]{article}
\usepackage{pgf-pie} 
\begin{document}
\begin{tikzpicture} 

\pie[rotate=180, text=inside]
 {46/Rice, 32/Wheat, 22/Other}
     
\end{tikzpicture}
\end{document}

输出:

使用Tikz的Latex条形图和饼图

上图中显示的颜色是Latex的默认颜色。你也可以修改颜色。下面列出了另一个可用于进一步修改的命令:

  • text = pin:在特定切片旁边, 通过一行连接文本。
  • text = legend:它产生一个单独的图例。
  • color = {blue!30, red!20, orange}:它为三个切片分别设置特定的颜色。
  • pos = 3, 4:根据所提到的x和y顶点定位图表。饼图的默认位置为(0, 0)。
  • sum = auto:用于根据切片的值计算总和。

注意:所有其他参数都应在\ pie命令之前的方括号内传递。

让我们考虑一个修改颜色并更改文本格式的示例。

下面给出了该示例的代码:

\documentclass[12pt]{article}
\usepackage{pgf-pie} 
\usepackage{xcolor} % package used to implement colors
\begin{document}
\begin{tikzpicture} 

\pie[rotate=270, text=pin, color={blue!30, red!40, orange!70}]
 {46/Rice, 32/Wheat, 22/Other}
 
\end{tikzpicture}
\end{document}

输出:

使用Tikz的Latex条形图和饼图

饼图类别中还有其他三种类型的图表, 即方形图表, 云图表和极坐标图。

  • 云图

可以通过在\ pie中包含cloud命令来创建云图表。

该代码将与饼图的代码相同。此处的区别在于\ pie命令中仅包含云。

代码如下:

\documentclass[12pt]{article}
\usepackage{pgf-pie} 
\usepackage{xcolor} % package used to implement colors
\begin{document}
\begin{tikzpicture} 

\pie[cloud, rotate=270, text=inside, color={blue!40, red!30, orange!50}]
 {46/Rice, 32/Wheat, 22/Other}
 
\end{tikzpicture}
\end{document}

输出:

使用Tikz的Latex条形图和饼图

方形图

可以通过在\ pie中包含square命令来创建方图。

该代码将与饼图的代码相同。此处的区别在于\ pie命令中仅包含正方形。

代码如下:

\documentclass[12pt]{article}
\usepackage{pgf-pie} 
\usepackage{xcolor} % package used to implement colors
\begin{document}
\begin{tikzpicture} 

\pie[square, rotate=270, text=inside, color={blue!40, red!30, orange!50}]
 {46/Rice, 32/Wheat, 22/Other}
 
\end{tikzpicture}
\end{document}

输出:

使用Tikz的Latex条形图和饼图
  • 极坐标图

极坐标图可以通过在\ pie中包含polar命令来创建。

该代码将与饼图的代码相同。此处的区别在于\ pie命令中仅包含Polar。

代码如下:

\documentclass[12pt]{article}
\usepackage{pgf-pie} 
\usepackage{xcolor} % package used to implement colors
\begin{document}
\begin{tikzpicture} 

\pie[polar, rotate=270, text=inside, color={blue!40, red!30, orange!50}]
 {46/Rice, 32/Wheat, 22/Other}
 
\end{tikzpicture}
\end{document}

输出:

使用Tikz的Latex条形图和饼图

你还可以使用explode命令浏览图表的切片。

下面给出了爆炸极图切片的代码:

\documentclass[12pt]{article}
\usepackage{pgf-pie} 
\usepackage{xcolor} % package used to implement colors
\begin{document}
\begin{tikzpicture} 

\pie[explode=0.2, polar, rotate=270, text=inside, color={blue!40, red!30, orange!50}] % you can modify the explode distance according to the requirements.
 {46/Rice, 32/Wheat, 22/Other}
 
\end{tikzpicture}
\end{document}

输出:

使用Tikz的Latex条形图和饼图

赞(0)
未经允许不得转载:srcmini » 使用Tikz的Latex条形图和饼图

相关推荐

评论 抢沙发

评论前必须登录!