嗨,我是 LaTeX 的新手,我在尝试使用 pgfplots 在 tikz 图片中绘制图表时遇到了一些问题。它们列在下面

嗨,我是 LaTeX 的新手,我在尝试使用 pgfplots 在 tikz 图片中绘制图表时遇到了一些问题。它们列在下面

问题:

  1) I want to remove the pesky grid lines from the bar plot.

  2) I want a separate bar plot some distance from the others with the labels n and n+1.        
     Similar to picture 1 below.
     
  3) I would like to shade the entire area above the curve of 1/x and underneath the bars.
     in a different colour. As well as sketch a line along the edge of 
     those bars, that will be above that of 1/x but converging towards it.  

  4) I also want to be able to shade the area between two points, underneath the graph of       
     1/x (2-4 for example) to depict the partial sums.

我已尽力尝试解决这些问题的前三题,但没有成功,如能得到任何帮助我将非常感谢,谢谢!

首先,这是我的代码:

\begin{tikzpicture}
\begin{axis}[
        grid=none,
        axis lines=left,
        major tick length=0,
        ymajorgrids=false,
        xmajorgrids=false,
        xmin=0,
        xmax=10,
        ymin=0,
        ymax=1.1,
        ybar interval=1,
        xticklabel style={/tikz/xshift=10pt},
        xtick={1,2,3,4,5,9},
        xticklabels={1,2,3,4,5,n},
        ytick=1,
        yticklabel=1,
]
\addplot[
    fill=gray!60,
]
    coordinates{
    (1,1)
    (2,1/2)
    (3,1/3)
    (4,1/4)
    (5,1/5)
    (6,1/6)
    (7,1/7)
};  
    coordinates{
    (9,1/9};    
\addplot[
    domain=0:10,
    smooth,
    samples=100,
    color=blue,
    line width=0.5mm,
]
{1/x};  
\end{axis}      
\end{tikzpicture} \\

我想要实现的是这样的:谐波级数和 1/x

我现在有的是:

谐波级数和 1/x 之间的面积

答案1

一个可能的解决方案:

\documentclass[border=3.141592]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
    \begin{tikzpicture}
\begin{axis}[
declare function = {f(\t)=(1/\t);},
    axis lines=left,
    ymin=0, ymax=1.15,  ylabel=$y$,
    xmin=0, xmax=10.5,  xlabel=$x$,
    ytick=\empty,
    extra y ticks=1,
    xtick={1,...,5,9},
    xticklabels={1,...,5,$n$},
    ticklabel style = {font=\footnotesize},
    no marks
            ]
        \addplot [domain=1:5,  ybar interval=1, fill=gray!30, samples=5]{f(x)};
        \addplot [domain=9:10, ybar interval=1, fill=gray!30, samples=2]{f(x)};
        \addplot+[domain=1:10, very thick, blue, samples=51] {f(x)};
\end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

\psStep使用以下命令来执行此操作非常简单pstricks-add

\documentclass[pdf, border=10pt, svgnames]{standalone}
\usepackage{fourier}
\usepackage{pstricks-add}

\begin{document}

    \psset{yunit=10cm, arrowinset=0.12, linejoin=1, algebraic, plotpoints=200}
    \begin{pspicture}(-0.9,-0.1)(10.2,1.1)
    \psaxes[ticks=y, labels=y, showorigin=false]{->}(0,0)(-0.9,-0.1)(10.2,1.1)[$x$,-120][$y$,-130]
    \psline[linestyle=dotted](1,1)(0,1)
    \multido{\i=1+1}{5}{\uput[d](\i, 0){\i}}
    \uput[d](7,0){$\cdots\vphantom{1}$}\uput[d](8,0){$n$}\uput[d](9,0){$n+\smash{1}$}
    \psset{StepType =sup, linecolor=MidnightBlue, fillcolor=Lavender}
    \psStep[ fillstyle=solid](1,6){5}{1/x}
    \psStep[fillstyle=solid](8,9){1}{1/x}
    \psplot[linewidth=1.5pt, linecolor=MediumVioletRed]{0.92}{10}{1/x}
    \end{pspicture}

\end{document} 

在此处输入图片描述

编辑:若要仅为曲线上方的条形图部分添加阴影,可以使用以下完整代码:

   \psset{yunit=10cm, arrowinset=0.12, linejoin=1, algebraic, plotpoints=200}
    \begin{pspicture}(-0.9,-0.1)(10.2,1.1)
    \psaxes[ticks=y, labels=y, showorigin=false]{->}(0,0)(-0.9,-0.1)(10.2,1.1)[$x$,-120][$y$,-130]
    \psline[linestyle=dotted](1,1)(0,1)
    \multido{\i=1+1}{5}{\uput[d](\i, 0){\i}}
    \uput[d](7,0){$\cdots\vphantom{1}$}\uput[d](8,0){$n$}\uput[d](9,0){$n+\smash{1}$}
    \psset{StepType =sup, linecolor=MidnightBlue, fillcolor=Lavender}
    \psStep[ fillstyle=solid](1,6){5}{1/x}
    \psStep[fillstyle=solid](8,9){1}{1/x}
    \pscustom[linestyle=none, fillstyle=solid, fillcolor=white]{psline(1,0)(1,1)\psplot{1}{9}{1/x}\psline(9,0.111)(9,0)(1,0)}
    \psStep(1,6){5}{1/x}
    \psStep(8,9){1}{1/x}
    \rput{-10}(7,0.155){$\dots$}
    \psplot[linewidth=1.5pt, linecolor=MediumVioletRed]{0.92}{10}{1/x}
    \end{pspicture}

在此处输入图片描述

相关内容