Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.