3

I am new to python just following a tutorial but the output is not same as expected matplotlib is not showing any thing on chart except the bars.

Here is the code

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
packets = ['1.', '2', '3', '4', '5']
testTime = [2.3,1.7,3.5,2.9,1.2]
plt.bar(packets,testTime)
plt.ylabel('Responsi time (Seconds.milliseconds)')
plt.xlabel('Packets')
plt.title("Response Time")
plt.show()

Here is the output screenshot

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Junaid Khalid
  • 811
  • 2
  • 11
  • 26
  • 1
    As @eshirvana says, the problem is the line `fig.add_axes([0,0,1,1])`, which is not necessary. But the reason it causes this to happen is because the actual plot takes up the full space of the image; change this line to `fig.add_axes([0.1, 0.1, 0.9, 0.9])` and it will also work fine. – Lecdi Jun 27 '22 at 17:55

2 Answers2

4

by this fig.add_axes([0,0,1,1]) the axes will cover the whole plot, remove it or add a fraction (less than 1) , and you will be fine :

import matplotlib.pyplot as plt

fig = plt.figure()
# ax = fig.add_axes([0,0,1,1])
packets = ['1.', '2', '3', '4', '5']
testTime = [2.3,1.7,3.5,2.9,1.2]
plt.bar(packets,testTime)
plt.ylabel('Responsi time (Seconds.milliseconds)')
plt.xlabel('Packets')
plt.title="Response Time"
plt.show()

output:

1

eshirvana
  • 23,227
  • 3
  • 22
  • 38
1

In addition to the answer by @eshirvana:

If you don't know what fractions to use for ax = fig.add_axes([0,0,1,1]), however, you do need the axes ax (e.g. for subplots), you can do the following:

ax = fig.add_subplot(111). This creates a grid of 1x1 and places the subplot at position 1 (a more detailed explanation of what the value passed to add_subplot means can be found here).

The documentation of add_subplot() can be found here. And an explanation for the