1. Windows、Linux、macOS
ribbon = ''.join(f'\033[38;5;{idx}m#' for idx in range(0, 256, 1))
print(ribbon)
终端彩虹
2. 十六进制颜色映射 RGB 三基色
# 十六进制基础调色板.
based_palette = [f'{idx:02x}' for idx in range(0, 16, 1)]
# 十六进制彩色调色板.
colored_palette = [
f'{r:02x}/{g:02x}/{b:02x}'
for r in [0] + [0x5f + 40 * n for n in range(0, 5, 1)]
for g in [0] + [0x5f + 40 * n for n in range(0, 5, 1)]
for b in [0] + [0x5f + 40 * n for n in range(0, 5, 1)]
]
# 十六进制灰度调色板.
grayscale_palette = [
f'{idx:02x}/{idx:02x}/{idx:02x}'
for idx in [0x08 + 10 * n for n in range(0, 24, 1)]
]
color_256 = based_palette + colored_palette + grayscale_palette
color_dict = {color: idx for idx, color in enumerate(color_256)}
print(f"\033[38;5;{color_dict['ff/d7/5f']}m【测试颜色】\033[0m")
3. 打印 C 语言数组
格式化且带有颜色的数组
# include <stdio.h>
# include <stdlib.h>
// 打印 double 型三维数组.
void PrintDouble3D(double*** arr, int dim1, int dim2, int dim3) {
printf("\033[32m{\033[0m");
int i;
for (i = 0; i < dim1 - 1; ++i) {
printf("\033[33m{\033[0m");
int j;
for (j = 0; j < dim2 - 1; ++j) {
printf("\033[31m{\033[0m");
int k;
for (k = 0; k < dim3 - 1; ++k) {
printf("%lf, ", arr[i][j][k]);
}
printf("%lf\033[31m}\033[0m, ", arr[i][j][k]);
}
printf("\033[31m{\033[0m");
int k;
for (k = 0; k < dim3 - 1; ++k) {
printf("%lf, ", arr[i][j][k]);
}
printf("%lf\033[31m}\033[0m\033[33m}\033[0m, ", arr[i][j][k]);
}
printf("\033[33m{\033[0m");
int j;
for (j = 0; j < dim2 - 1; ++j) {
printf("\033[31m{\033[0m");
int k;
for (k = 0; k < dim3 - 1; ++k) {
printf("%lf, ", arr[i][j][k]);
}
printf("%lf\033[31m}\033[0m, ", arr[i][j][k]);
}
printf("\033[31m{\033[0m");
int k;
for (k = 0; k < dim3 - 1; ++k) {
printf("%lf, ", arr[i][j][k]);
}
printf("%lf\033[31m}\033[0m\033[33m}\033[0m\033[32m}\033[0m\n", arr[i][j][k]);
}
// 打印 double 型二维数组.
void PrintDouble2D(double** arr, int dim1, int dim2) {
for (int i = 0; i < dim1; ++i) {
for (int j = 0; j < dim2; ++j) {
printf("%lf\t", arr[i][j]);
}
printf("\n");
}
}
// 打印 double 型一维数组.
void PrintDouble1D(double* arr, int dim1) {
printf("{");
for (int i = 0; i < dim1 - 1; ++i) printf("%lf, ", arr[i]);
printf("%lf}\n", arr[dim1 - 1]);
}
int main(int argc, char* argv[], char** env) {
system("");
// 打印一维数组.
// ---------------------------------------------------
double C1[4] = {1.2, 3.4, 2.8, 12};
printf("---------------------------------------------------\n");
printf("1D Array is:\t");
PrintDouble1D(C1, 4);
printf("---------------------------------------------------\n");
// 打印二维数组.
// ---------------------------------------------------
int rows = 2;
int cols = 4;
double C2[2][4] = {
{1, 2, 3, 4.8},
{5.6, 6, 7, 8}
};
double* matrix[rows];
for (int i = 0; i < rows; ++i) matrix[i] = C2[i];
printf("2D Array is:\n");
PrintDouble2D(matrix, 2, 4);
printf("---------------------------------------------------\n");
// 打印三维数组.
// ---------------------------------------------------
int dim1 = 3, dim2 = 4, dim3 = 4;
double C[3][4][4] = {
{
{5.1, 3.5, 1.4, 0.2},
{4.9, 3.0, 1.4, 0.2},
{5.0, 3.3, 1.4, 0.2}
},
{
{4.9, 2.4, 3.3, 1.0},
{6.6, 2.9, 4.6, 1.3}
},
{
{6.5, 3.0, 5.5, 1.8},
{7.7, 3.8, 6.7, 2.2},
{7.2, 3.2, 6.0, 1.8},
{6.4, 2.8, 5.6, 2.1}
}
};
double*** ptr = (double***) malloc(dim1 * sizeof(double**));
for (int i = 0; i < dim1; ++i) {
ptr[i] = (double**) malloc(dim2 * sizeof(double*));
for (int j = 0; j < dim2; ++j) {
ptr[i][j] = (double*) malloc(dim3 * sizeof(double));
for (int k = 0; k < dim3; ++k) {
ptr[i][j][k] = C[i][j][k];
}
}
}
printf("3D Array is:\t");
PrintDouble3D(ptr, dim1, dim2, dim3);
printf("---------------------------------------------------\n");
for (int i = 0; i < dim1; ++i) {
for (int j = 0; j < dim2; ++j) {
free(ptr[i][j]);
}
free(ptr[i]);
}
free(ptr);
return 0;
}
4. C 语言进度条
#include <stdio.h>
void progress_bar(int current, int epoch, int step, char *description) {
if (current != 0 && current % step != 0 && current != epoch) return;
int width = 50;
// printf("\x1b[?25l");
float percentage = (epoch > 0) ? (float)current / epoch : 0.0F;
int filled = percentage * width;
printf("\r[");
for (int j = 0; j < width; j++) {
if (j < filled) printf("\x1b[32m=\x1b[0m");
else if (j == filled) printf(">");
else printf(" ");
}
printf("] %7.2f%% (%d/%d)", percentage * 100, current, epoch);
if (description != NULL && description[0] != '\0') printf(" \x1b[33m%s\x1b[0m\x1b[K", description);
else printf("\x1b[K");
fflush(stdout);
if (current >= epoch) printf("\n");
// if (current >= epoch) printf("\x1b[?25h\n");
}
int main(int argc, char *argv[], char *envs[]) {
int epoch = 100000;
for (int i = 0; i < epoch; i++) {
progress_bar(i + 1, epoch, 1, NULL);
}
printf("============= 1 =============\n");
for (int i = 0; i < epoch; i++) {
progress_bar(i + 1, epoch, 10, "This is a description.");
}
printf("============= 2 =============\n");
return 0;
}
5. LaTeX 条形图
% pdflatex main.tex
% dvisvgm --no-fonts --pdf --exact main.pdf -o main.svg
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.18}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{matrix, calc}
\definecolor{color1}{HTML}{EAE0D5}
\definecolor{color2}{HTML}{C6D5C5}
\definecolor{color3}{HTML}{82B0AA}
\definecolor{color4}{HTML}{4A8B93}
\definecolor{color5}{HTML}{00505F}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style = {
group size = 2 by 1,
horizontal sep = 1cm
},
ybar = 0pt,
ymin = 0,
ymax = 100,
width = 7cm,
height = 7cm,
/pgf/bar width = 8pt,
ymajorgrids = true,
grid style = dashed,
xtick = data,
symbolic x coords = {SR, E2E},
enlarge x limits = 0.5,
tick label style = {font = \small},
]
\nextgroupplot[
xlabel = {
\textit{
\textbf{Heat food in the microwave}
}
}
] {
\addplot[fill = color1] coordinates {(SR, 2) (E2E, 20)};
\addplot[fill = color2] coordinates {(SR, 12) (E2E, 35)};
\addplot[fill = color3] coordinates {(SR, 28) (E2E, 65)};
\addplot[fill = color4] coordinates {(SR, 65) (E2E, 95)};
\addplot[fill = color5] coordinates {(SR, 65) (E2E, 88)};
}
\nextgroupplot[
xlabel = {
\textit{
\textbf{Packing detergent from conveyor}
}
}
] {
\addplot[fill = color1] coordinates {(SR, 5) (E2E, 25)};
\addplot[fill = color2] coordinates {(SR, 8) (E2E, 28)};
\addplot[fill = color3] coordinates {(SR, 48) (E2E, 80)};
\addplot[fill = color4] coordinates {(SR, 25) (E2E, 45)};
\addplot[fill = color5] coordinates {(SR, 75) (E2E, 90)};
}
\end{groupplot}
\node at (
$(group c1r1.north) ! 0.5 ! (group c2r1.north) + (0, 1em)$
) [anchor = south] {
\begin{tikzpicture}
\matrix[
draw = none,
matrix of nodes,
column sep = 0.5em,
row sep = 0.3cm,
nodes = {
anchor = center,
inner sep = 0.5em
}
] {
| [fill = color1, minimum width = 1em, minimum height = 1em] | & Category 1 &
| [fill = color2, minimum width = 1em, minimum height = 1em] | & Category 2 &
| [fill = color3, minimum width = 1em, minimum height = 1em] | & Category 3 &
| [fill = color4, minimum width = 1em, minimum height = 1em] | & Category 4 &
| [fill = color5, minimum width = 1em, minimum height = 1em] | & Category 5 \\
};
\end{tikzpicture}
};
\end{tikzpicture}
\end{document}
6. LaTeX 函数图
% pdflatex main.tex
% dvisvgm --no-fonts --pdf --exact main.pdf -o main.svg
% https://www.overleaf.com/learn/latex/Pgfplots_package
\documentclass{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
title = Exmple using the mesh parameter,
axis lines = left,
xlabel = {Temperature $x$},
ylabel = $f(x)$,
zlabel = $z$,
xtick = {0, 20, 40, 50, 60, 80, 100},
ytick = {0, 20, 40, 60, 80, 100},
xmin = 0,
xmax = 100,
ymin = 0,
ymax = 120,
% hide axis,
xmajorgrids = true,
ymajorgrids = true,
grid style = dashed,
colormap/cool,
legend style = {
at = {(0.75, 1)},
anchor = north west,
nodes = {
inner sep = 7pt,
anchor = center
},
fill = white,
draw = green
}
]
\addplot3[
mesh,
samples = 50,
domain = -8 : 8,
] {
sin(deg(sqrt(x^2 + y^2))) / sqrt(x^2 + y^2)
};
\addlegendentry{$\dfrac{\sin(r)}{r}$}
\addplot[color = red]{exp(x)};
\addlegendentry{$\exp(x)$}
\addplot [
domain = -10 : 10,
samples = 100,
color = red,
] {
x^2 - 2*x - 1
};
\addlegendentry{\(x^2 - 2x - 1\)}
\addplot [
domain = -10 : 10,
samples = 100,
color = blue,
] {
x^2 + 2*x + 1
};
\addlegendentry{\(x^2 + 2x + 1\)}
\addplot[
color = blue,
mark = square,
] coordinates {
(0,23.1) (10,27.5) (20,32) (30,37.8) (40,44.6) (60,61.8) (80,83.8) (100,114)
};
\addlegendentry{CuSO\(_4\cdot\)5H\(_2\)O}
\end{axis}
\end{tikzpicture}
\end{document}
7. LaTeX Morandi 表格
% pdflatex main.tex
% dvisvgm --no-fonts --pdf --exact main.pdf -o main.svg
\documentclass{standalone}
\usepackage{tikz}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\newcommand{\cube}[1]{
\begin{tikzpicture}[baseline = 0ex]
\draw[
draw = #1,
fill = #1!20,
thick,
rounded corners = 0.5pt
] (0,0) rectangle (0.25, 0.25);
\end{tikzpicture}
}
\definecolor{MorandiLightCyan}{RGB}{209, 238, 238}
\definecolor{MorandiBlue}{RGB}{70, 110, 255}
\definecolor{MorandiGrey}{RGB}{80, 100, 100}
\definecolor{MorandiPurple}{RGB}{140, 110, 180}
\definecolor{MorandiCyan}{RGB}{110, 170, 190}
\definecolor{MorandiRed}{RGB}{220, 130, 130}
\definecolor{MorandiOrange}{RGB}{250, 210, 160}
\begin{document}
% \begin{table}[!t]
\setlength{\tabcolsep}{12pt}
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{clcccc}
\bottomrule[1.5pt]
\@ & \textbf{Model} & \textbf{Scene} & \textbf{Motion} & \textbf{Semantics} & \textbf{Score} \\
\midrule[0.5pt]
\rowcolor{MorandiLightCyan}\cube{MorandiBlue} & GE-Base & 0.9427 & 1.6676 & 2.0907 & 4.7010 \\
\cube{MorandiGrey} & Kling & 0.8888 & 0.9440 & 2.0370 & 3.8698 \\
\cube{MorandiPurple} & Hailuo & 0.8577 & 0.5362 & 2.0186 & 3.4125 \\
\cube{MorandiCyan} & COSMOS & 0.7963 & 0.7085 & 1.7824 & 3.2872 \\
\cube{MorandiRed} & OpenSora & 0.9210 & 0.3442 & 1.8739 & 3.1392 \\
\cube{MorandiOrange} & LTX & 0.9156 & 0.4002 & 1.6518 & 2.9676 \\
\toprule[1.5pt]
\end{tabular}
% \end{table}
\end{document}
8. LaTeX 散点图
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
colorbar,
enlargelimits = false,
xlabel = {GPA},
ylabel = {ma},
]
\addplot + [
only marks,
scatter,
mark = halfcircle*,
mark size = 3pt,
point meta = \thisrow{co}
] table[x = GPA, y = un] {
GPA ma ve co un
3.45 643 589 3.76 3.52
2.78 558 512 2.87 2.91
2.52 583 503 2.54 2.4
3.67 685 602 3.83 3.47
3.24 592 538 3.29 3.47
2.1 562 486 2.64 2.37
};
\end{axis}
\end{tikzpicture}
% \begin{tikzpicture}
% \begin{axis}[
% colorbar,
% enlargelimits = false,
% xlabel = {GPA},
% ylabel = {ma},
% ]
% \addplot + [
% only marks,
% scatter,
% mark = halfcircle*,
% mark size = 3pt,
% point meta = \thisrow{co}
% ] table [x = GPA, y = ma] {mydata.dat};
% \end{axis}
% \end{tikzpicture}
\end{document}
9. LaTeX 彩色表格
\documentclass[varwidth]{standalone}
\usepackage[table]{xcolor}
\usepackage{pifont}
\usepackage{amsmath}
\usepackage{multirow}
\definecolor{cadetblue}{HTML}{5F9EA0}
\definecolor{darksalmon}{rgb}{0.91, 0.59, 0.48}
\definecolor{forestgreen}{rgb}{0.13, 0.55, 0.13}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\begin{document}
\setlength{\tabcolsep}{12pt}
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{C{4em}M{4em}|cc|cc}
\rowcolor{cadetblue!20} & & \multicolumn{2}{c}{\textbf{dim3}} & \multicolumn{2}{c}{\textbf{dim4}} \\
\rowcolor{cadetblue!20} \multirow{-2}{*}{\hspace{1.5em}\textbf{dim1}} & \multirow{-2}{*}{\textbf{dim2}} & $\mathcal{S}$ & $\mathcal{T}$ & $\mathcal{P}$ & $\mathcal{Q}$ \\
\hline
\textcolor{darksalmon}{\ding{55}} & \textcolor{darksalmon}{\ding{55}} & 0.1 & 0.2 & 0.3 & 0.4 \\
\rowcolor{gray!10} \textcolor{darksalmon}{\ding{55}} & \textcolor{forestgreen}{\ding{51}} & 0.4 & 0.3 & 0.2 & 0.1 \\
\textcolor{forestgreen}{\ding{51}} & \textcolor{darksalmon}{\ding{55}} & 0.5 & 0.6 & 0.7 & 0.8 \\
\rowcolor{gray!10} \textcolor{forestgreen}{\ding{51}} & \textcolor{forestgreen}{\ding{51}} & \textbf{0.9} & 0.2 & \textbf{0.8} & 0.7 \\
\end{tabular}
\end{document}