1

I want to plot one value on a bar plot but the bar is very thick, I want the bar to be thinner.

import matplotlib.pyplot as plt

# Data for the bar plot
x = ['Category']
y = [10]

# Create a figure and axis with a narrower width
fig, ax = plt.subplots(figsize=(4, 4))

# Adjust the width of the bars
bar_width = 0.3  # Decrease this value to make the bars thinner

# Plot the bars
ax.bar(x, y, width=bar_width)

# Show the plot
plt.show() 

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

1 Answers1

2

One way to do this is by using xlim(). As you have categorical x-axis, it is set at 0. So, you can use limit of -1 and 1 to make it thinner. The more the difference, thinner the bar...

x = ['Category'] 
y = [10]
fig, ax = plt.subplots(figsize=(4, 4))
bar_width = 0.3
ax.bar(x, y, width=bar_width)
ax.set_xlim(-1, 1)
plt.show()

enter image description here

Another way to change the perspective of the bar width, relative to the x-axis, is to increase the x-axis margins with ax.margins(x=1)


Given x = ['Category'], ax.get_xticklabels() results in [Text(0, 0, 'Category')].

Given x = [0], ax.get_xticklabels() results in

[Text(-0.2, 0, '−0.20'),
 Text(-0.15000000000000002, 0, '−0.15'),
 Text(-0.1, 0, '−0.10'),
 Text(-0.04999999999999999, 0, '−0.05'),
 Text(0.0, 0, '0.00'),
 Text(0.04999999999999999, 0, '0.05'),
 Text(0.10000000000000003, 0, '0.10'),
 Text(0.15000000000000002, 0, '0.15'),
 Text(0.2, 0, '0.20')]

Which does a better job of showing the width of the bar in relation to the range of the x-axis ticks. The bar is the same width, but the range of the x-axis is larger.

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Redox
  • 9,321
  • 5
  • 9
  • 26