Ask Your Question
1

How can two separate bar charts be positioned adjacent to each other in Python?

asked 2022-07-23 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-06-23 01:00:00 +0000

david gravatar image

You can use the Matplotlib library in Python to create two separate bar charts and then position them adjacent to each other using subplots.

Here's an example code:

import matplotlib.pyplot as plt

# create data for bar charts
x1 = ['A', 'B', 'C', 'D', 'E']
y1 = [10, 15, 12, 8, 6]

x2 = ['F', 'G', 'H', 'I', 'J']
y2 = [9, 7, 13, 11, 5]

# create two subplots with bar charts
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10,5))

ax1.bar(x1, y1)
ax2.bar(x2, y2)

# add labels and titles to subplots
ax1.set_xlabel('Letters')
ax1.set_ylabel('Values')
ax1.set_title('Bar Chart 1')

ax2.set_xlabel('Letters')
ax2.set_ylabel('Values')
ax2.set_title('Bar Chart 2')

# adjust spacing between subplots
plt.subplots_adjust(wspace=0.4)

# display the plot
plt.show()

In this example, we first create two sets of data for the bar charts (x1, y1, x2, y2). Then we create two subplots using subplot(), with 1 row and 2 columns. This creates two side-by-side subplots. We also specify the figsize to control the size of the figure.

We then use the bar() function to create bar charts in each of the subplots. We also set labels and titles for each subplot, and adjust the spacing between the subplots using subplots_adjust().

Finally, we display the plot using the show() function.

This will create two separate bar charts positioned adjacent to each other in Python.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-07-23 11:00:00 +0000

Seen: 7 times

Last updated: Jun 23 '22