1

I want to plot two figures in one image using matplotlib. Data which I want to plot is:

x1 = ['sale','pseudo','test_mode']
y1 = [2374064, 515, 13]

x2 = ['ready','void']
y2 = [2373078, 1514]

I want to plot the bar plot for both the figure in one image. I used the code given below:

f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x1, y1)
ax1.set_title('Two plots')
ax2.plot(x2, y2)

but its giving error:

ValueError: could not convert string to float: PSEUDO

How I can plot them in one image using matplotlib?

2 Answers 2

1

Try this:

x1 = ['sale','pseudo','test_mode']
y1 = [23, 51, 13]

x2 = ['ready','void']
y2 = [78, 1514]

y = y1+y2
x = x1+x2
pos = np.arange(len(y))
plt.bar(pos,y)
ticks = plt.xticks(pos, x)

Separate figures in one image:

x1 = ['sale','pseudo','test_mode']
y1 = [23, 51, 13]

x2 = ['ready','void']
y2 = [78, 1514]

fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)

pos1 = np.arange(len(y1))
ax1.bar(pos1,y1)
plt.sca(ax1)
plt.xticks(pos1,x1)

pos2 = np.arange(len(y2))
ax2.bar(pos,y2)
plt.sca(ax2)
plt.xticks(pos2,x2)
Sign up to request clarification or add additional context in comments.

1 Comment

I can't draw like this. x1 and x2 are two different attributes. From your solution it will get plot as a one attribute. I want to make to separate figures for x1 and x2. Separate figure in one image.
0

The problem is that your x values are not numbers, but text. Instead, plot the y values and then change the name of the xticks (see this answer):

import matplotlib.pyplot as plt

x1 = ['sale','pseudo','test_mode']
y1 = [23, 51, 13]

x2 = ['ready','void']
y2 = [78, 1514]

f, axes = plt.subplots(1, 2, sharey=True)
for (x, y, ax) in zip((x1, x2), (y1, y2), axes):
    ax.plot(y)
    ax.set_xticks(range(len(x))) # make sure there is only 1 tick per value
    ax.set_xticklabels(x)
plt.show()

This produces:

line graph

For a bar graph, switch out ax.plot(y) with ax.bar(range(len(x)), y). This will produce the following:

bar graph

4 Comments

It's giving error AttributeError: 'AxesSubplot' object has no attribute 'xticks'
@neha I was in a hurry, and did not have time to verify the code. I've updated the example.
Did you know how I can plot the bar plot of this two diagrams?
@neha I added a bar graph as well. Consider accepting this answer if it solves your problem.

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.