[Matplotlib-users] Categorical variables and twinx make basically no sense

Jody Klymak jklymak at uvic.ca
Thu Mar 14 01:29:08 EDT 2019



> On Mar 13, 2019, at  21:40 PM, AJ M <ajm8671 at gmail.com> wrote:
> 
> The only conclusion I can come to is that matplotlib treats these values differently, and converts the text arrays to integers under the hood without trying to align them.

Thats basically correct to my understanding - the twin axes is a new axes that shares it’s xlimits with the old one, but we don’t have any way of passing “categories” from one axes to the next, so it carries its own list of category->integer conversion that gets made anew when you call scatter.  The first axes is the one that gets the tick labels.  

You *may* be able to pass the converter to the second axes, but I’m not sure.  

Easier would be to just do the following:

```python

import matplotlib.pyplot as plt
import numpy as np

y1 = [5, np.NaN, 6, np.NaN, 15]
y2 = [100, 200, 300, 400, 500]

x = ['apples','carrots','bananas','watermelon','cheerios']

fig, ax = plt.subplots()
ax.scatter(x, y2)
ax2 = ax.twinx()
ax2.scatter(x, y1, color = "orange")
plt.show()
```

Cheers, Jody
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190313/42b53449/attachment.html>


More information about the Matplotlib-users mailing list