0

I show bar chart image with this code.

print(df['a'][1:].astype(int).plot.bar())

The image is very small I try to resize image by using matplotlib like this code.

plt.figure(figsize=(4,3))
plt.plot(df['a'][1:].astype(int).sum())

when I run I show empty image without bar chart. How to fix it?

user58519
  • 579
  • 1
  • 4
  • 19

4 Answers4

0

The reason that it is empty is that your df['a'][1:], is a 1d vector (a pd.Series). The sum of that, is a scalar. You cannot plot a scalar.

I think for a bar chart, you need plt.bar(df['a'][1:]).

As for the figure size, you have the figsize command, like you note in the question.

Surya Narayanan
  • 418
  • 6
  • 9
0

Maybe you can give, even bigger integer parameters to the figsize

For instance:

plt.figure(figsize=(18, 5))

Here is an example:

import matplotlib.pyplot as plt

f, ax = plt.subplots(figsize=(18,5)) # set the size that you'd like (width, height)
plt.bar([1,2,3,4], [0.1,0.2,0.3,0.4], label = 'first bar')
plt.bar([5,6,7,8], [0.4,0.3,0.2,0.1], label = 'second bar')
ax.legend(fontsize = 14)
plt.show()
Ahmet
  • 7,527
  • 3
  • 23
  • 47
0

i would basically compute the data for x and y before. It seems that the plot is empty because it is only a scalar. The following code works:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame(np.array([1,2,3,4,5,5,6]), columns=['x'])
df['y'] = df['x'][1:].astype(int).sum()

#Change your fig size here
fig = plt.figure(figsize=(10,8))
plt.bar(df.x, df.y)
plt.show()
Nles
  • 161
  • 4
0

You could set the size with figsize

import matplotlib.pyplot as plt

f, ax = plt.subplots(figsize=(18,5)) # set the size that you'd like (width, height)
plt.bar([1,2,3,4], [0.1,0.2,0.3,0.4], label = 'first bar')
plt.bar([10,11,12,13], [0.4,0.3,0.2,0.1], label = 'second bar')
ax.legend(fontsize = 14)

enter image description here

Ujjwal Dash
  • 767
  • 1
  • 4
  • 8