Constructed a graph on three points in jupiter notebook

import matplotlib.pyplot as plt import pandas as pd %matplotlib inline table = [ [0,-2,2], [3,0,6], ['A(0;3)','B(-2;0)','C(2;6)'] ] df = pd.DataFrame(table, index=['x','y','Точка'], columns=['A','B','C']) X = [x for x in df.loc['x']] Y = [y for y in df.loc['y']] plt.plot(X, Y, marker='o'); plt.annotate('A(0;3)', xy=(df['A']['x'] + 0.2,df['A']['y'])) plt.annotate('B(-2;0)', xy=(df['B']['x'] + 0.2,df['B']['y'])) plt.annotate('C(2;6)', xy=(df['C']['x'] + 0.2,df['C']['y'])); 

How to sign the points on the chart, that is, near each point put A (0; 3), B (-2; 0), C (2; 6) 'corresponding signature? I did it in a curve through the annotation, I'm sure you don’t have to do it, how to sign the points correctly?

    1 answer 1

    I would do it like this:

    first transpose the DF for convenience:

     In [217]: dft = df.T In [218]: dft Out[218]: xy Точка A 0 3 A(0;3) B -2 0 B(-2;0) C 2 6 C(2;6) 

    We draw the points connected by segments:

     In [219]: plt.plot(dft['x'], dft['y'], marker='o') Out[219]: [<matplotlib.lines.Line2D at 0xcd4d588>] In [220]: ax = plt.gca() 

    add signatures:

     In [221]: dft.apply(lambda x: ax.annotate(x['Точка'], (x['x'] + 0.2, x['y'])), axis=1) Out[221]: A Annotation(0.2,3,'A(0;3)') B Annotation(-1.8,0,'B(-2;0)') C Annotation(2.2,6,'C(2;6)') dtype: object 

    Result:

    enter image description here

    first we get the object ax

     ax = plt.gca() 

    From docstring:

     In [227]: plt.gca? Signature: plt.gca(**kwargs) Docstring: Get the current :class:`~matplotlib.axes.Axes` instance on the current figure matching the given keyword args, or create one. 

    then add signatures:

    dft.apply(func, axis=1) - executes / calls the func function for each row ( axis=1 ) DataFrame. Those. ax.annotate() called with the appropriate parameters for each row.

    • Explain these methods ax = plt.gca () and dft.apply - Igor Igoryanych