Home > Back-end >  Plotting multiple line graphs in matplotlib
Plotting multiple line graphs in matplotlib

Time:02-01

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:

Output

CodePudding user response:

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.

CodePudding user response:

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.

CodePudding user response:

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 enter image description here

This also adds labels to the lines.

CodePudding user response:

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()

enter image description here

CodePudding user response:

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 Fancy image number 42

  •  Tags:  
  • Related