27

I am trying to use Python to create a scatter plot that contains two X categories "cat1" "cat2" and each category has multiple Y values. I can get this to work if the number of Y values for each X value is the same by using this following code:

    import numpy as np
    import matplotlib.pyplot as plt

    y = [(1,1,2,3),(1,1,2,4)]
    x = [1,2]
    py.plot(x,y)
    plot.show()

but as soon as the number of Y values for each X value is not the same, I get an error. For example this does not work:

    import numpy as np
    import matplotlib.pyplot as plt

    y = [(1,1,2,3,9),(1,1,2,4)] 
    x = [1,2]
    plt.plot(x,y)
    plot.show()
    #note now there are five values for x=1 and only four for x=2. error

How can I plot different numbers of Y values for each X value and how can I change the X axis from being the numbers 1 and 2 to text categories "cat1" and "cat2". I would greatly appreciate any help on this!

Here is a sample image of the type of plot I am trying to make:

http://s12.postimg.org/fa417oqt9/pic.png

3
  • 1
    How do you see that happen? Can you make a draft for your desired plot? Perhaps you want to add some np.nan? Commented Dec 15, 2015 at 3:15
  • I added a picture to my original post of the type of plot I hope to make. Commented Dec 15, 2015 at 3:34
  • your link to a plot is no longer valid Commented Oct 24, 2023 at 20:22

2 Answers 2

41

How can I plot different numbers of Y values for each X value

Just plot each group separately:

for xe, ye in zip(x, y):
    plt.scatter([xe] * len(ye), ye)

and how can I change the X axis from being the numbers 1 and 2 to text categories "cat1" and "cat2".

Set ticks and tick labels manually:

plt.xticks([1, 2])
plt.axes().set_xticklabels(['cat1', 'cat2'])

Full code:

import matplotlib.pyplot as plt
import numpy as np

y = [(1,1,2,3,9),(1,1,2,4)]
x = [1,2]

for xe, ye in zip(x, y):
    plt.scatter([xe] * len(ye), ye)

plt.xticks([1, 2])
plt.axes().set_xticklabels(['cat1', 'cat2'])

plt.savefig('t.png')

enter image description here

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

1 Comment

So I take it there really is not a direct way to plot an array of dependent variables for a single independent variable?
7

To respond to the comment from javadba, I was able to plot multiple dependent variables (new covid cases) for a single independent variable (date) using matplotlib.

plt.xticks(rotation=90)
plt.scatter(x=dates_uk[::5], y=cases_uk[::5])
plt.scatter(x=dates_us[::5], y=cases_us[::5])
classes = ['UK New Cases', 'US New Cases']
plt.legend(labels=classes)
plt.show()

Image of graph that code produces

Comments

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.