Reputation: 199
I'm using matplotlib to draw line graphs and whenever I try to draw the second line graph, the y-axis gets printed two times.
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5]
y1 = ['1000', '13k', '26k', '42k', '60k', '81k']
y2 = ['1000', '13k', '27k', '43k', '63k', '85k']
plt.plot(x, y1)
plt.plot(x, y2, '-.')
plt.xlabel("X-axis data")
plt.ylabel("Y-axis data")
plt.title('multiple plots')
plt.show()
This is the code I'm using, what am I doing wrong.
Output:
Upvotes: 18
Views: 104397
Reputation: 699
if you are finding out how to do the same using pandas,
import pandas as pd
_df = pd.DataFrame({
"x": [0, 1, 2, 3, 4, 5],
"y1": [1000, 13000, 26000, 42000, 60000, 81000],
"y2": [1000, 13000, 27000, 43000, 63000, 85000]
})
_df.plot(x="x", y=["y1", "y2"], xlabel="X-axis data",
ylabel="Y-axis data", title="Multiple plots")
Note that it does support y=["y1","y2"]
which does the magic of having multiple lines on the plot.
Upvotes: 1
Reputation: 4588
There are a lot of answers here already, but for completeness, here is how you can show two different set of ticks at the same time using twinx
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5]
# We don't wanna plot the strings
y1 = [1_000, 13_000, 26_000, 42_000, 60_000, 81_000]
y2 = [1_000, 13_000, 27_000, 43_000, 63_000, 85_000]
# But we do want to show them
y1_ticks = ['1k', '13k', '26k', '42k', '60k', '81k']
y2_ticks = ['1k', '13k', '27k', '43k', '63k', '85k']
plt.plot(x, y1)
plt.plot(x, y2, '-.')
plt.yticks(y1, y1_ticks)
# This is just another ax on the same plot
# `twinx` just creates a shared x-axis for two y-axes
ax2 = plt.gca().twinx()
ax2.set_yticks(y2)
ax2.set_yticklabels(y2_ticks)
ax2.set_ylabel("Another Y-axis data")
plt.xlabel("X-axis data")
plt.ylabel("Y-axis data")
plt.title('multiple plots')
plt.show()
Upvotes: 4
Reputation: 482
I assume that you want to put the line into 1 graph. This is the result when I am using integer of your data. I think SiP already make a script to convert array of string into array of integer for you
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5]
y1 = [1000, 13000, 26000, 42000, 60000, 81000]
y2 = [1000, 13000, 27000, 43000, 63000, 85000]
plt.plot(x, y1)
plt.plot(x, y2, '-.')
plt.xlabel("X-axis data")
plt.ylabel("Y-axis data")
plt.title('multiple plots')
plt.show()
Upvotes: 0
Reputation: 3009
Change the two lines:
y1 = ['1000', '13k', '26k', '42k', '60k', '81k']
y2 = ['1000', '13k', '27k', '43k', '63k', '85k']
to
y1 = [1000, 13000, 26000, 42000, 60000, 81000]
y2 = [1000, 13000, 27000, 43000, 63000, 85000]
It consists of two change. First, plot
does not plot a string. Second, you change 'k' to multiples of 1000
.
Try the following:
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5]
y1 = [1000, 13000, 26000, 42000, 60000, 81000]
y2 = [1000, 13000, 27000, 43000, 63000, 85000]
plt.plot(x, y1, label ='y1')
plt.plot(x, y2, '-.', label ='y2')
plt.xlabel("X-axis data")
plt.ylabel("Y-axis data")
plt.legend()
plt.title('multiple plots')
plt.show()
This also adds labels to the lines.
Upvotes: 12
Reputation: 1159
Your y values are strings instead of numbers, matplotlib lets you plot them but there is no "number" scale to the plot so it simply add the new labels (strings like '85k') on top. A simple fix would be to replace the 'k' in all the strings with 'e3' and then cast all the values to a number using float().
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5]
y1 = ['1000', '13k', '26k', '42k', '60k', '81k']
y2 = ['1000', '13k', '27k', '43k', '63k', '85k']
Now convert and plot:
plt.plot(x, [float(i.replace('k', 'e3')) for i in y1])
plt.plot(x, [float(i.replace('k', 'e3')) for i in y2], '-.')
plt.xlabel("X-axis data")
plt.ylabel("Y-axis data")
plt.title('multiple plots')
plt.show()
Here I converted the strings just for the purpose of plotting.
Upvotes: 12
Reputation: 313
The problem is the Y-axis data you are providing to the plot. Of course you can plot poth lines in one plot, just by changing the y1, y2
variables. Try the following code:
import matplotlib.pyplot as plt
x = [0, 1, 2, 3, 4, 5]
y1 = [1000, 13000, 26000, 42000, 60000, 81000]
y2 = [1000, 13000, 27000, 43000, 63000, 85000]
plt.plot(x, y1)
plt.plot(x, y2, '-.')
plt.xlabel("X-axis data")
plt.ylabel("Y-axis data")
plt.title('multiple plots')
plt.show()
If this is not wat you want and you want to hake the "k" extension to the number, you can change the y-labels as described here.
Upvotes: 0
Reputation: 406
This code that you are using is for a single plot. In single plot it will draw two lines for graph. To draw to different plots in one code statement.
you can use:
fig,ax = plt.subplots(2)
then use:
ax[0].plot(x,y1)
ax[1].plot(x,y2)
or if you want you can separate your code into two blocks of code.
Upvotes: 0