0

I am trying to create a horizontal barblot with mathplotlib, but I've ran into several problems.

  1. I need to change the size of the whole plot. Currently it is 800x600 by default. I'd like to set the parameters myself and so that the plot would resize into it then. Because for example currently there is too much empty space between the bars and I'd like to enlarge the width of the bars.

  2. If the text (bar header or numeric value) doesn't fit into the plot, it will be left out. I'd like to extend the screen so that the texts would never dissapear.

Here's the sample image that the code outputs: enter image description here

Here's the sample code:

import numpy as np
import matplotlib.pyplot as plt

people = ('Tom sadf hasidfu hasdufhas d', 'Dick', 'Harry', 'Slim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))

plt.barh(y_pos, performance, align='center', height=0.3, color="skyblue")
plt.yticks(y_pos, people)
plt.xlabel('Performance')
plt.title('How fast do you want to go today?')

for i, v in enumerate(performance):
    plt.text(v + 0.5, i, str(v), color='blue', fontweight='bold')

#plt.show()

filename = "myfilename.png"
plt.savefig(filename)

1 Answer 1

3

There are a lot of ways to change the figure size and adjust the parameters of the plot. They can all be found using the seach engine of choice.

To give an example, the figure size may be changed via the figsize argument, the ticklabels can be included by calling plt.tight_layout and the limits can be set via plt.xlim.

import numpy as np
import matplotlib.pyplot as plt

people = ('Tom Hanswurst Fitzgerald', 'Dick', 'Harry', 'Slim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))

plt.figure(figsize=(8,4))
plt.barh(y_pos, performance, align='center', height=0.3, color="skyblue")
plt.yticks(y_pos, people)
plt.xlim(0,np.max(performance)*1.4)
plt.xlabel('Performance')
plt.title('How fast do you want to go today?')

for i, v in enumerate(performance):
    plt.text(v + 0.5, i, str(v), color='blue', fontweight='bold')

plt.tight_layout()
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.