如何在使用 pgfdeclareplotmark 的 pgfplot hexbin 图上绘制一条额外的线

如何在使用 pgfdeclareplotmark 的 pgfplot hexbin 图上绘制一条额外的线

我非常喜欢六边形图,并使用杰克的代码我设法避免使用 R 来渲染它。

现在,我确实有一个问题,如何添加在其上方而不是下方绘制的线图?

梅威瑟:

用于生成箱子的 R 代码:

# Load the data
library(maps)
data(world.cities)

# Load the hexbin package
library(hexbin)

# Generate hexbins, with the aspect ratio of the plot matching that of the data.
hbin<-hexbin(x=world.cities$long,y=world.cities$lat,xbins=100,shape=diff(range(world.cities$lat))/diff(range(world.cities$long)))

# Write the datafile. hcell2xy extracts the centroids of the hexagons
write.table(data.frame(hcell2xy(hbin),slot(hbin,"count")),row.name=F,file="testdata.csv")

文本代码:

\documentclass{standalone}
\usepackage{siunitx}
\usepackage{pgfplots, pgfplotstable}

\sisetup{round-mode=places,round-precision=0}

\pgfdeclareplotmark{hexagon}
{%
  \pgfpathmoveto{\pgfqpoint{0pt}{1.1547\pgfplotmarksize}}
  \pgfpathlineto{\pgfqpointpolar{150}{1.1547\pgfplotmarksize}}
  \pgfpathlineto{\pgfqpointpolar{210}{1.1547\pgfplotmarksize}}
  \pgfpathlineto{\pgfqpointpolar{270}{1.1547\pgfplotmarksize}}
  \pgfpathlineto{\pgfqpointpolar{330}{1.1547\pgfplotmarksize}}
  \pgfpathlineto{\pgfqpointpolar{30}{1.1547\pgfplotmarksize}}
  \pgfpathclose
\pgfusepathqfill
}

\pgfplotsset{
colormap={grayred}{color(0cm)=(black!10); color(1cm)=(red!75!black)}
}


\begin{document}
\begin{tikzpicture}[]
\begin{axis}[
    enlargelimits=false,
    colorbar, colormap name=grayred,
           scale only axis,width=10cm,unit vector ratio*=1 1 1,
    enlarge x limits={abs=2},enlarge y limits={abs=2},
    xlabel=Longitude, ylabel=Latitude, xticklabel={\SI{\tick}{\degree}},yticklabel={\SI{\tick}{\degree}},
]
\addplot [
    scatter, scatter/use mapped color={draw=mapped color, fill=mapped color},
    scatter src=explicit,
    only marks,
    mark=hexagon,mark size=\pgfkeysvalueof{/pgfplots/width}/100/2
] table [meta index=2] {testdata.csv};

\addplot [blue, domain=-150:150] {0.5*x};
\end{axis}
\end{tikzpicture}


\end{document}

示例图片

问题:如何让蓝线图绘制在自定义绘图标记上?

答案1

不将标记绘制在其他所有内容之上的一个更简单的方法是简单地使用clip mode=individual

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        % ---------------------------------------------------------------------
        % to *not* draw the markers on a separate layer on top of the lines
        clip mode=individual,
        % ---------------------------------------------------------------------
    ]
        \addplot [
            red,
            samples=10,
            only marks,
            mark=*,
        ]{.5*x};
        \addplot [
            blue,
            very thick,
        ]{0.5*x};
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

答案2

您可以将其用于set layers环境axison layer=axis foreground蓝线。

这是一个简单的例子(我不能使用 R 代码):

\documentclass{standalone}
\usepackage{pgfplotstable}% loads also pgfplots
\pgfplotsset{compat=1.14}% <- add this, current version is 1.14

\pgfdeclareplotmark{hexagon}
{%
  \pgfpathmoveto{\pgfqpoint{0pt}{1.1547\pgfplotmarksize}}
  \pgfpathlineto{\pgfqpointpolar{150}{1.1547\pgfplotmarksize}}
  \pgfpathlineto{\pgfqpointpolar{210}{1.1547\pgfplotmarksize}}
  \pgfpathlineto{\pgfqpointpolar{270}{1.1547\pgfplotmarksize}}
  \pgfpathlineto{\pgfqpointpolar{330}{1.1547\pgfplotmarksize}}
  \pgfpathlineto{\pgfqpointpolar{30}{1.1547\pgfplotmarksize}}
  \pgfpathclose
\pgfusepathqfill
}

\begin{document}
\begin{tikzpicture}[]
\begin{axis}[
    set layers,% <- add this
    width=10cm
]
\addplot [
    red,
    samples=10,
    only marks,
    mark=hexagon,
    mark size=\pgfkeysvalueof{/pgfplots/width}/100
  ]{.5*x};
\addplot [
    blue,
    very thick,
    on layer=axis foreground% <- add this
  ]{0.5*x};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容