18

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

1
  • 1
    I think it is because you are using string instead of integer. '1000' is string, so the program can not know that '1000'<'13k'. try using integer may fix your problem Commented Feb 1, 2022 at 8:52

7 Answers 7

13

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

enter image description here

This also adds labels to the lines.

Sign up to request clarification or add additional context in comments.

Comments

12

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.

Comments

4

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

Fancy image number 42

Comments

1

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.

multi-line line graph (pyplot) using pandas

Comments

0

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.

Comments

0

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.

Comments

0

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

2 Comments

A solution that suggests manual changes to the data defies the purpose of a minimal reproducible example. Your solution only works for the example data.
ah sorry, I only thought that he was confused between string and integer. for the the converter I think SiP already made the converter

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.