40

I am using matplotlib.pyplot to plot a graph from a Dataframe.

I want to show the height of the bar upon each rectangle and am using Text(). For normalising the y-axis, I have taken the log. Below is my code, I am getting error

Image size of 1005x132589 pixels is too large. It must be less than 2^16 in each direction

When I am not using plt.yscale('log') then the code is working fine.

According to some suggestions, I have restarted my kernel too, but still getting this issue. Any suggestions regarding this is welcomed.

My Code:

# data collected to list. 
list_alarms = df_region.alarmName
# list_east = df_region.EAST
list_west = df_region.WEST
list_north = df_region.NORTH
list_south = df_region.SOUTH


# X-ticks customization
N = len(list_alarms)
xpos = np.arange(N)

# this json file is used to update the style of the plot. 
s = json.load(open('style.json'))    
rcParams.update(s)

# Graph customize
plt.rcParams['figure.figsize'] = (15,8)
plt.xlabel('AlarmNames at different Regions')
plt.ylabel('Frequency for alarms in MNG-PAN devices')
plt.title('Alarm Generated by MNG-PAN Device at different Regions')
plt.xticks(xpos, list_alarms, rotation = 280)

# bar1 = plt.bar(xpos - 0.3,list_east, width = 0.2, label = 'EAST', color = '#3154C8')
bar2 = plt.bar(xpos - 0.1, list_west, label = 'WEST', color = '#FF9633')
bar3 = plt.bar(xpos + 0.1, list_north, label = 'NORTH', color ='#12B794')
bar4 = plt.bar(xpos + 0.3, list_south, label = 'SOUTH', color ='#EF6FE3')
plt.yscale('log')
plt.legend()

def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        if height < 10000:
            plt.text(rect.get_x() + rect.get_width()/2., 1.05*height, 
                 '%d'%int(height),
                 ha = 'center', va = 'bottom', rotation = 90, 
    fontsize=9)


# # autolabel(bar1)
autolabel(bar2)
autolabel(bar3)
autolabel(bar4)

plt.show()

I am using Jupyter Notebook, Pandas, Python3.

dspencer
  • 4,297
  • 4
  • 22
  • 43
Debashis Sahoo
  • 5,388
  • 5
  • 36
  • 41

6 Answers6

40

Check your location variables at plt.text line in autolabel definition. I had the same problem and I found that the y variable I gave to plt.text function was too large compared to the value of my dataset of plot.

This is also an issue with plt.annotate if the location is outside the figure.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
johanna
  • 401
  • 4
  • 6
7

Your X axis might be string values and plt.text() only take scalar values to move on the axis. So that's why your range might be beyond the image size. Convert the x-axis to scalar values. I had same problem, I did the same.

5

It worked for me using the transform=ax.transAxes keyword argument:

for example:

plt.text(0.5, 0.5, 'Some text', transform=ax.transAxes)
pppery
  • 3,731
  • 22
  • 33
  • 46
Createdd
  • 865
  • 11
  • 15
3

You can adujust pixels and update your update_datalim()

fig = plt.figure(1, figsize=(8, 14), frameon=False, dpi=100)
fig.add_axes([0, 0, 1, 1])
ax = plt.gca()

corners = ((x1, y1), (x2, y2))
ax.update_datalim(corners)
Phoenix
  • 104
  • 6
3

This happens because your x-axis contains dates and when you're adding text, you're passing scalar values.

To solve this, you should convert dates with matplotlib.mdates.date2num() Refer to this answer: Annotate Time Series plot in Matplotlib

1

Another possible explanation could be that one of your y-axis values is zero. Thus you could edit your autolabel function into something like this:

def autolabel(rects):
for rect in rects:
    height = rect.get_height()
    if (height != 0):
        ax.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')
Reinier Koops
  • 765
  • 5
  • 6