Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
values on graph
#1
Hi All,

I'm currently studying how to create graphs. My problem is, I can't show y axis values in my graph. My code is the below

##
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import datetime

df1 = pd.read_csv("/Users/accuservebusinesssystems/Downloads/19SVN_DA.csv")
df1['Packing_date'] = pd.to_datetime(df1['Packing_date'], errors='coerce') # transforms packing date to time
df1["Bottles"] = df1["Bottles"].apply(lambda x: float(x.split()[0].replace(',', ''))) #remove commas from string
bottle = pd.pivot_table(df1, values = "Bottles", index = df1["Packing_date"].dt.strftime("%m"), aggfunc=np.sum) #packing_date to number in months
bottle.index = bottle.index.astype(str).astype(int) #transform string index to int

#graph
ax = plt.figure().add_subplot(111)

x = bottle["Packing_date"]
y = bottle["Produced_bottles"]
plt.plot(x,y)
for i,j in zip(x,y):
    ax.annotate(str(j),xy=(i,j))
    
ticklabels = [datetime.date(1900, item, 1).strftime('%b') for item in bottle.index]
ax.set_xticks(np.arange(1,13))
ax.set_xticklabels(ticklabels) #add monthlabels to the xaxis

ax.legend(bottle.columns.tolist(), loc='center left', bbox_to_anchor=(1, .5)) #add the column names as legend.

plt.show()

##
My error appears to be the below

Error:
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) ~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2645 try: -> 2646 return self._engine.get_loc(key) 2647 except KeyError: pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'Packing_date' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) <ipython-input-14-c67c62db627e> in <module> 2 ax = plt.figure().add_subplot(111) 3 ax.plot(bottle) ----> 4 x = bottle["Packing_date"] 5 y = bottle["Produced_bottles"] 6 plt.plot(x,y) ~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in __getitem__(self, key) 2798 if self.columns.nlevels > 1: 2799 return self._getitem_multilevel(key) -> 2800 indexer = self.columns.get_loc(key) 2801 if is_integer(indexer): 2802 indexer = [indexer] ~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2646 return self._engine.get_loc(key) 2647 except KeyError: -> 2648 return self._engine.get_loc(self._maybe_cast_indexer(key)) 2649 indexer = self.get_indexer([key], method=method, tolerance=tolerance) 2650 if indexer.ndim > 1 or indexer.size > 1: pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'Packing_date'
Thank you. I appreciate the help in advance.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to do bar graph with positive and negative values different colors? Mark17 1 5,002 Jun-10-2022, 07:38 PM
Last Post: Mark17
  function accepts infinite parameters and returns a graph with those values edencthompson 0 812 Jun-10-2022, 03:42 PM
Last Post: edencthompson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020