Tikz 条形图和饼图

Tikz 条形图和饼图 首页 / Latex入门教程 / Tikz 条形图和饼图

条形图

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

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

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

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

\documentclass[12pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=6.6cm,compat=1.7}
\begin{document}
\title{How to learn latex graphs pie charts using tikz} 
\author{ Welcome learnfk.com } 
\date{\today}
\maketitle

\begin{tikzpicture}
    \begin{axis}
    [
        ybar,
        enlargelimits=0.1,
        ylabel={\#Ages}, % ylabel 必须在# 符号之前。
        xlabel={\#LearnFK IP},
        symbolic x coords={Google, Apple, Hary, Liza, LearnFK}, % 这些是 x 轴坐标的规范。
        xtick=data,
         nodes near coords, % 此命令用于提及特定条形顶部的 y 轴点。
        nodes near coords align={vertical},
        ]
        \addplot coordinates {(Google,99) (Apple,90) (Hary,70) (Liza,80) (LearnFK,66) };
    \end{axis}
\end{tikzpicture}
\end{document}

输出:

Latex 条形图 and 饼状图 using Tikz

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

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

\documentclass[12pt]{article} % 
\usepackage{pgfplots} %用于实现绘图的包
\pgfplotsset{width=6.5cm, compat=1.6}  
\begin{document}  
    \title{How to learn latex graphs equations using tikz} 
    \author{ Welcome learnfk.com } 
    \date{\today}
    \maketitle
    \begin{tikzpicture}  
        \begin{axis}  
        [  
            ybar, % ybar 命令以水平形式显示图形,而 xbar 命令以垂直形式显示图形。
            enlargelimits=0.15,% 这些限制用于缩小或扩大图表。限制越小,图表扩展或增长得越高。限制越大,图形缩小得越多。  
            legend style={at={(0.4,-0.25)}, % 这些是包含剩余(小麦、茶、大米)的底行的度量,其中 -0.25 是底行与图表之间的差距。
              anchor=north,legend columns=-1},     
              % 在这里,北是底部图例行的位置。您可以指定东、西或南方向来移动位置。 
            ylabel={\#Annual Growth Percentage}, % 此处的行之间不应有行距。否则,会显示错误。 
            symbolic x coords={2016, 2017, 2018},  
            xtick=data,  
            nodes near coords,  
            nodes near coords align={vertical},  
            ]  
        \addplot coordinates {(2016, 75) (2017, 78) (2018, 80)}; % 这些是特定条形图的度量。 y 轴的刻度线会根据坐标中输入的数据值自动调整。
        \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}  

输出:

Latex 条形图 and 饼状图 using Tikz

饼状图

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

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

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

\documentclass[12pt]{article}
\usepackage{pgf-pie} % 用于实现饼图的包
\begin{document}

\title{How to learn latex graphs equations using tikz} 
\author{ Welcome learnfk.com } 
\date{\today}
\maketitle
\begin{tikzpicture} % Tikz environment

\pie
 {46/Rice,
     32/Wheat, 22/Other} % 必须重新检查所有提到的组件的总和是否应为 100%。否则,Latex 会将左边的百分比留空。
     
\end{tikzpicture}
\end{document}

输出:

Latex 条形图 and 饼状图 using Tikz

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

此类示例的代码如下:

\documentclass[12pt]{article}
\usepackage{pgf-pie} % 用于实现饼图的包
\begin{document}

\title{How to learn latex graphs equations using tikz} 
\author{ Welcome learnfk.com } 
\date{\today}
\maketitle
\begin{tikzpicture} % Tikz environment

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

输出:

Latex 条形图 and 饼状图 using Tikz

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

\documentclass[12pt]{article}
\usepackage{pgf-pie} % 用于实现饼图的包
\begin{document}

\title{How to learn latex graphs equations using tikz} 
\author{ Welcome learnfk.com } 
\date{\today}
\maketitle
    \begin{tikzpicture} % Tikz environment
    
        \pie[rotate=180, text=inside]
         {46/Rice,
             32/Wheat, 22/Other}
         
    \end{tikzpicture}
\end{document}

输出:

Latex 条形图 and 饼状图 using Tikz

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

无涯教程网

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

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

我们来看一个修改颜色并更改文本格式的示例。

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

\documentclass[12pt]{article}
\usepackage{pgf-pie} % 用于实现饼图的包
\usepackage{xcolor} % 引入颜色包
\begin{document}

\title{How to learn latex graphs equations using tikz} 
\author{ Welcome learnfk.com } 
\date{\today}
\maketitle
    \begin{tikzpicture} % Tikz environment
    
       \pie[rotate=270, text=pin, color={blue!30, red!40, orange!70}]
 {46/Rice,
     32/Wheat, 22/Other}
         
    \end{tikzpicture}
\end{document}

输出:

Latex 条形图 and 饼状图 using Tikz

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

  • 云图

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

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

代码如下:

\documentclass[12pt]{article}
\usepackage{pgf-pie} % 用于实现饼图的包
\usepackage{xcolor} % 引入颜色包
\begin{document}

\title{How to learn latex graphs equations using tikz} 
\author{ Welcome learnfk.com } 
\date{\today}
\maketitle
    \begin{tikzpicture} % Tikz environment
        
       \pie[cloud,rotate=270, text=pin, color={blue!30, red!40, orange!70}]
 {46/Rice,
     32/Wheat, 22/Other} %只多了一个cloud
         
    \end{tikzpicture}
\end{document}

输出:

Latex 条形图 and 饼状图 using Tikz

方形图

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

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

代码如下:

\documentclass[12pt]{article}
\usepackage{pgf-pie} % 用于实现饼图的包
\usepackage{xcolor} % 引入颜色包
\begin{document}

\title{How to learn latex graphs equations using tikz} 
\author{ Welcome learnfk.com } 
\date{\today}
\maketitle
    \begin{tikzpicture} % Tikz environment
        
       \pie[square,rotate=270, text=pin, color={blue!30, red!40, orange!70}]
 {46/Rice,
     32/Wheat, 22/Other} %只多了一个square
         
    \end{tikzpicture}
\end{document}

输出:

Latex 条形图 and 饼状图 using Tikz
  • 极坐标图

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

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

代码如下:

\documentclass[12pt]{article}
\usepackage{pgf-pie} % 用于实现饼图的包
\usepackage{xcolor} % 引入颜色包
\begin{document}

\title{How to learn latex graphs equations using tikz} 
\author{ Welcome learnfk.com } 
\date{\today}
\maketitle
    \begin{tikzpicture} % Tikz environment
        
       \pie[polar,rotate=270, text=pin, color={blue!30, red!40, orange!70}]
 {46/Rice,
     32/Wheat, 22/Other} %只多了一个polar
         
    \end{tikzpicture}
\end{document}

输出:

Latex 条形图 and 饼状图 using Tikz

您还可以使用 explode 命令浏览图表的各个部分。

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

\documentclass[12pt]{article}
\usepackage{pgf-pie} % 用于实现饼图的包
\usepackage{xcolor} % 引入颜色包
\begin{document}

\title{How to learn latex graphs equations using tikz} 
\author{ Welcome learnfk.com } 
\date{\today}
\maketitle
    \begin{tikzpicture} % Tikz environment
        
       \pie[explode=0.2, polar, rotate=270, text=inside, color={blue!40, red!30, orange!50}] % 您可以根据需要修改距离。
 {46/Rice,
     32/Wheat, 22/Other}
    \end{tikzpicture}
\end{document}

输出:

Latex 条形图 and 饼状图 using Tikz

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

AI技术内参 -〔洪亮劼〕

朱赟的技术管理课 -〔朱赟〕

雷蓓蓓的项目管理实战课 -〔雷蓓蓓〕

Service Mesh实战 -〔马若飞〕

打造爆款短视频 -〔周维〕

手把手带你写一门编程语言 -〔宫文学〕

反爬虫兵法演绎20讲 -〔DS Hunter〕

结构学习力 -〔李忠秋〕

程序员职业规划手册 -〔雪梅〕

好记忆不如烂笔头。留下您的足迹吧 :)