29

As you can see the code produces a barplot that is not as clear and I want to make the figure larger so the values can be seen better. This doesn't do it. What is the correct way? x is a dataframe and x['user'] is the x axis in the plot and x['number'] is the y.

import matplotlib.pyplot as plt
%matplotlib inline  
plt.bar(x['user'], x['number'], color="blue")
plt.figure(figsize=(20,10)) 

The line with the plt.figure doesn't change the initial dimensions.

user10341548
  • 337
  • 1
  • 4
  • 9
  • 10
    try the `plt.figure` line before the `plt.bar` line. – tda Sep 11 '18 at 11:16
  • Thanks. How to put it in the same line with the barplot? Can this be ? – user10341548 Sep 11 '18 at 11:19
  • I've now added my comment as an answer for completeness. I'm unsure what you mean, if you've an additional problem it may be best to create a new question providing a minimal, complete and verifiable example. See here: https://stackoverflow.com/help/mcve – tda Sep 11 '18 at 11:21

2 Answers2

48

One option (as mentioned by @tda), and probably the best/most standard way, is to put the plt.figure before the plt.bar:

import matplotlib.pyplot as plt

plt.figure(figsize=(20,10)) 
plt.bar(x['user'], x['number'], color="blue")

Another option, if you want to set the figure size after creating the figure, is to use fig.set_size_inches (note I used plt.gcf here to get the current figure):

import matplotlib.pyplot as plt

plt.bar(x['user'], x['number'], color="blue")
plt.gcf().set_size_inches(20, 10)

It is possible to do this all in one line, although its not the cleanest code. First you need to create the figure, then get the current axis (fig.gca), and plot the barplot on there:

import matplotlib.pyplot as plt

plt.figure(figsize=(20, 10)).gca().bar(x['user'], x['number'], color="blue")

Finally, I will note that it is often better to use the matplotlib object-oriented approach, where you save a reference to the current Figure and Axes and call all plotting functions on them directly. It may add more lines of code, but it is usually clearer code (and you can avoid using things like gcf() and gca()). For example:

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(20, 10))
ax = fig.add_subplot(111)
ax.bar(x['user'], x['number'], color="blue")
Alex K.
  • 103
  • 8
tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • what is gcf? I can't find it explained anywhere on the net – user10341548 Sep 11 '18 at 11:31
  • I wasn't aware of the `gcf` option - nice! Worth noting that, although the final solution is all on one line, it actually adds a processing step (`gca()`) and the execution order is unchanged. – tda Sep 11 '18 at 11:32
  • `gcf` gets a reference to the current figure instance (I've given you a link to the docs). And yes you have another step (`gca`: get the current Axes), but internally when you use `plt.bar` it will do that anyway so matplotlib knows which Axes to plot on – tmdavison Sep 11 '18 at 11:34
  • If you use the `matplotlib` object-oriented approach you can avoid using `gcf()` and `gca()`: `fig = plt.figure(figsize=(20, 10); ax = fig.add_subplot(111); ax.bar(...)` See my edit above about this. – tmdavison Sep 11 '18 at 11:35
2

Try setting up the size of the figure before assigning what to plot, as below:

import matplotlib.pyplot as plt
%matplotlib inline  

plt.figure(figsize=(20,10)) 
plt.bar(x['user'], x['number'], color="blue")
tda
  • 2,045
  • 1
  • 17
  • 42
  • I mean to put the figsize in one line during the `plt.bar(x['user'], x['number'], color="blue")` as a parameter in this line. Is it possible? – user10341548 Sep 11 '18 at 11:23
  • 1
    There is no need? Making everything on one line doesn't necessarily make it better code. Anyway, I don't think this is possible as you need to create the figure object before assigning the data to plot. – tda Sep 11 '18 at 11:24
  • "*Try setting up the size of the figure before assigning what to plot*", it doesn't work for me, but I saw instructions in Matplotlib behave inconsistently from one case to the other, in particular the same sequence which works in one case, doesn't work in another case, in spite there is no obvious difference. Sometimes it is possible to access the same feature (at least partially) using another object or with a call rather than an assignment. This seems a low consistency library, which often needs "bypasses", and the documentation doesn't show a clear way to do things. – mins Oct 28 '20 at 21:48