我如何才能使条形图的填充与右侧颜色条的颜色同步?它们有点偏移……
\documentclass[border=0.5mm,12pt]{standalone}
\usepackage{pgfplots,pgfplotstable} % Grafici
\pgfplotsset{%
mesh line legend/.style={legend image code/.code=\meshlinelegend#1},%
/pgf/number format/use comma,%
compat=newest,%
% height=9cm,%
width=12cm%
}
\begin{document}
%\begin{figure}
\centering
\begin{tikzpicture}
\makeatletter \newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis} \makeatother
\pgfplotsset{axis line on top/.style={
axis on top=false,
after end axis/.append code={
\pgfplotsset{axis line style=opaque,
ticklabel style=opaque,
tick style=opaque,
grid=none}
\pgfplotsdrawaxis}
}
}
\pgfdeclareverticalshading{stefan}{50bp}{
color(0.00000000000000bp)=(violet);
color(8.33333333333333bp)=(blue);
color(16.66666666666670bp)=(cyan);
color(25.00000000000000bp)=(green);
color(33.33333333333330bp)=(yellow);
color(41.66666666666670bp)=(orange);
color(50.00000000000000bp)=(red)
}
\begin{axis}[%
xtick=data,
/pgf/number format/fixed,
legend pos=outer north east,
ymajorgrids=true,
%ybar,
%x tick label as interval,
enlarge x limits=false,
x filter/.code=\pgfmathparse{#1-0.5},
%xmin=0, xmax=7,
ymin=0, ymax=14,
point meta min={0},
point meta max={14},
yticklabel shift=2pt,
xticklabel shift=2pt,
minor ytick={0,0.4,...,14},
xticklabel style={text height=1.5ex},
xtick={0,1,...,7},
extra x ticks={0,1,...,7},
extra x tick style={
xticklabel pos=right,
xticklabel style={text depth=0pt}
},
%extra y ticks={0,2,...,12},
%extra y tick style={
% yticklabel pos=right
%},
tick style={thin,black},
xlabel=\large Classe,
ylabel=\large Frequenza,
colorbar,
colorbar style={
/pgf/number format/fixed,
xticklabel shift=2pt,
xticklabel style={text height=1.5ex}
},
axis line style={draw=none}, after end axis/.append code={\draw (rel axis cs:0,0) rectangle (rel axis cs:1,1);},
colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)}
]
\addplot[%
ybar interval,
area style,
shading=stefan,
% draw=none,
shader=interp,
samples=10,
draw=black
] coordinates{%
(1, 6)
(2, 9)
(3, 11)
(4, 12)
(5, 7)
(6, 5)
(7, 0)
};
\end{axis}
%\end{figure}
\end{tikzpicture}
\end{document}
答案1
其中一种选择是这样的:
\documentclass[border=0.5mm,12pt]{standalone}
\usepackage{pgfplots,pgfplotstable} % Grafici
\pgfplotsset{%
mesh line legend/.style={legend image code/.code=\meshlinelegend#1},%
/pgf/number format/use comma,%
compat=newest,%
% height=9cm,%
width=12cm%
}
\begin{document}
%\begin{figure}
\centering
\begin{tikzpicture}
\makeatletter \newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis} \makeatother
\pgfplotsset{axis line on top/.style={
axis on top=false,
after end axis/.append code={
\pgfplotsset{axis line style=opaque,
ticklabel style=opaque,
tick style=opaque,
grid=none}
\pgfplotsdrawaxis}
}
}
\pgfdeclareverticalshading{stefan}{50bp}{
color(0.00000000000000bp)=(violet);
color(8.33333333333333bp)=(blue);
color(16.66666666666670bp)=(cyan);
color(25.00000000000000bp)=(green);
color(33.33333333333330bp)=(yellow);
color(41.66666666666670bp)=(orange);
color(50.00000000000000bp)=(red)
}
\begin{axis}[%
xtick=data,
/pgf/number format/fixed,
legend pos=outer north east,
ymajorgrids=true,
%ybar,
%x tick label as interval,
enlarge x limits=false,
x filter/.code=\pgfmathparse{#1-0.5},
%xmin=0, xmax=7,
ymin=0, ymax=14,
point meta min={0},
point meta max={12}, % original:14
yticklabel shift=2pt,
xticklabel shift=2pt,
minor ytick={0,0.4,...,14},
xticklabel style={text height=1.5ex},
xtick={0,1,...,7},
extra x ticks={0,1,...,7},
extra x tick style={
xticklabel pos=right,
xticklabel style={text depth=0pt}
},
%extra y ticks={0,2,...,12},
%extra y tick style={
% yticklabel pos=right
%},
tick style={thin,black},
xlabel=\large Classe,
ylabel=\large Frequenza,
colorbar,
colorbar style={
/pgf/number format/fixed,
xticklabel shift=2pt,
xticklabel style={text height=1.5ex},
ymax=14%\pgfkeysvalueof{/pgfplots/point meta max},
},
axis line style={draw=none}, after end axis/.append code={\draw (rel axis cs:0,0) rectangle (rel axis cs:1,1);},
colormap={new}{color(0cm)=(violet);color(1cm)=(blue);
color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);
color(5cm)=(orange);color(6cm)=(red)}
]
\addplot[%
ybar interval,
area style,
shading=stefan,
% draw=none,
shader=interp,
samples=10,
draw=black
] coordinates{%
(1, 6)
(2, 9)
(3, 11)
(4, 12)
(5, 7)
(6, 5)
(7, 0)
};
\end{axis}
%\end{figure}
\end{tikzpicture}
\end{document}
另一个选择是:
\documentclass[border=0.5mm,12pt]{standalone}
\usepackage{pgfplots,pgfplotstable} % Grafici
\pgfplotsset{%
mesh line legend/.style={legend image code/.code=\meshlinelegend#1},%
/pgf/number format/use comma,%
compat=newest,%
% height=9cm,%
width=12cm%
}
\begin{document}
%\begin{figure}
\centering
\begin{tikzpicture}
\makeatletter \newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis} \makeatother
\pgfplotsset{axis line on top/.style={
axis on top=false,
after end axis/.append code={
\pgfplotsset{axis line style=opaque,
ticklabel style=opaque,
tick style=opaque,
grid=none}
\pgfplotsdrawaxis}
}
}
\pgfdeclareverticalshading{stefan}{50bp}{
color(0.00000000000000bp)=(violet);
color(8.33333333333333bp)=(blue);
color(16.66666666666670bp)=(cyan);
color(25.00000000000000bp)=(green);
color(33.33333333333330bp)=(yellow);
color(41.66666666666670bp)=(orange);
color(50.00000000000000bp)=(red)
}
\begin{axis}[%
xtick=data,
/pgf/number format/fixed,
legend pos=outer north east,
ymajorgrids=true,
%ybar,
%x tick label as interval,
enlarge x limits=false,
x filter/.code=\pgfmathparse{#1-0.5},
%xmin=0, xmax=7,
ymin=0, ymax=12,
point meta min={0},
point meta max={12},
yticklabel shift=2pt,
xticklabel shift=2pt,
minor ytick={0,0.4,...,14},
xticklabel style={text height=1.5ex},
xtick={0,1,...,7},
extra x ticks={0,1,...,7},
extra x tick style={
xticklabel pos=right,
xticklabel style={text depth=0pt}
},
%extra y ticks={0,2,...,12},
%extra y tick style={
% yticklabel pos=right
%},
tick style={thin,black},
xlabel=\large Classe,
ylabel=\large Frequenza,
colorbar,
colorbar style={
/pgf/number format/fixed,
xticklabel shift=2pt,
xticklabel style={text height=1.5ex}
},
axis line style={draw=none}, after end axis/.append code={\draw (rel axis cs:0,0) rectangle (rel axis cs:1,1);},
colormap={new}{color(0cm)=(violet);color(1cm)=(blue);color(2cm)=(cyan);color(3cm)=(green);color(4cm)=(yellow);color(5cm)=(orange);color(6cm)=(red)}
]
\addplot[%
ybar interval,
area style,
shading=stefan,
% draw=none,
shader=interp,
samples=10,
draw=black
] coordinates{%
(1, 6)
(2, 9)
(3, 11)
(4, 12)
(5, 7)
(6, 5)
(7, 0)
};
\end{axis}
%\end{figure}
\end{tikzpicture}
\end{document}