24382

Adding data labels to linechart [duplicate]

<h3>Question</h3>
<b>This question already has answers here</b>: Label python data points on plot (2 answers) Closed 2 years ago. </aside>

I would like to add data labels on top of the circles of this graph with pandas/matplotlib.

The data is loaded from an Excel file using pandas and 'Month' as index

#Importing the data Path = 'xyz.xlsx' df = pd.read_excel(Path,sheetname=0,index_col='Month')

I then proceed to plot the data using the following code

plt.plot(df['Sales1'],marker='o',label='Sales') plt.show()

I have tried annotating, but can't get it to work.

my dataframe looks like this

Sales1 Sales2 Sales3 Month 2015-08-01 24457 31895 42081 2015-09-01 6926 43584 20666 2015-10-01 4973 4845 10962 2015-11-01 21345 17909 36115 2015-12-01 8639 40668 38215 2016-01-01 48021 18145 25353 2016-02-01 6708 24651 46089 2016-03-01 8827 18617 31215 2016-04-01 49703 14205 26983 2016-05-01 3223 16658 1854 2016-06-01 6484 46503 13523 2016-07-01 41243 18876 20740 2016-08-01 21779 13362 48997 2016-09-01 9494 40242 15477 2016-10-01 1205 10291 32663 2016-11-01 42655 41375 48549 2016-12-01 24644 26002 6602 2017-01-01 33732 44292 45151 2017-02-01 47880 15503 1404 2017-03-01 32499 17755 11135 2017-04-01 42888 31527 25174 2017-05-01 34433 8292 20117 2017-06-01 9884 2359 45424 2017-07-01 35302 24177 48045
<h3>Answer1:</h3>

Not exactly sure what you tried but annotate should work. Something similar to the solution discussed here should do the trick

import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame(pd.np.random.randn(5,1)*10 + 100, index=pd.date_range("2015-01-01", "2015-01-05"), columns=["Sales1"]).round() fig = plt.figure() ax = fig.add_subplot(111) plt.plot(df['Sales1'], marker='o', label='Sales') for i,j in df.Sales1.items(): ax.annotate(str(j), xy=(i, j)) plt.show()

来源:https://stackoverflow.com/questions/46063077/adding-data-labels-to-linechart

Recommend

  • CSS Ignore one element when determining parent size [duplicate]
  • The “pull-nested-quantifiers” option seems to cause problems in the context for UFBV?
  • Using PDFs for icon images in Xcode 7.2
  • A “regex for words” (semantic replacement) - any example syntax and libraries?
  • How to find the index of the minimum value between two specific columns of a matrix with Rcpp?
  • pan gesture issue, slight movement within UIButton detection
  • Proper implementation of producer-consumer scenario and “graceful” termination of thread pool
  • opencv error: assertion failed (size.width>0 && size.height>0) in unknown function lin
  • How to avoid “Merge branch 'master' of ssh://gdcm.git.sourceforge.net/gitroot/gdcm/gdcm”
  • Rails 4: Using PostgreSQL function in order causes error in query due to the includes table not bein
  • Outlook PropertyFrom MAPI Schema Property ID
  • Laravel - Collection with relations take a lot of time
  • How to add an Auth Token in every request using AFIncrementalStore?
  • Updating a 'master' JSON object by adding data to a subobject
  • How to signing Android APK
  • getsockname return invalid address
  • Esper window usage: Recalculation based on event leaving window
  • Regular expression breakpoint in GDB
  • How to make html video autoplay on phones and tablets?
  • Android: Compressing images creates black borders on left and top margin
  • How to force Delphi to use D8.bat instead of dx.bat to compile Java 1.8 bytecode into DEX bytecode
  • how to animate two continuous popup windows with jquery mobile?
  • GitLab Continuous Deployment to Azure Web App
  • How to set a dynamic form fields to dirty or touched with angular?
  • How to debug component/typescript code when running Protractor
  • PHP users local time
  • Overwrite text file programmatically
  • MFMailComposer send email without presenting view
  • How to convert days into months using datetime in Python3?
  • Year over Year Stats from a Crossfilter Dataset
  • Create an average of multiple excel chart without the data source
  • How to turn off notice reporting in xampp?
  • how to run ejabberd with Erlang on Heroku?
  • How to use Streams api peek() function and make it work?
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • Access to a Matlab gui from the web