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

AJ M ajm8671 at gmail.com
Thu Mar 14 00:40:47 EDT 2019


Example code is pasted below. Basically, just wanted to say that this is
either completely non-intuitive, or I am failing to understand something
fundamental about matplotlib.

My impression would be that twinx would let me assign categorical values to
however they were before I twinned them (i.e., using the original axis'
order and coordinates). But, if you use categorical values, it will
truncate them to the length of the categorical list/array. In the example
code, if you comment out the x1/x2 categorical assignments and use integers
instead (uncomment those), it works as you'd expect. You can even swap the
order in which the integers are plotted, but NOT if you use the text
assignments, despite the fact that the integer and text axes are the same.

Gallery: https://imgur.com/a/NTgCFXB

Anyway, someone please let me know if there is some design principle I'm
missing here, or if this is a special case. It's missing from any of the
top level documentation (see:
https://matplotlib.org/gallery/lines_bars_and_markers/categorical_variables.html
, https://matplotlib.org/examples/api/two_scales.html ) and took me the
better part of this afternoon to figure out. 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.

Cheers,

AJ

Code:

import matplotlib.pyplot as plt

x1 = ['apples', 'bananas', 'cheerios']
##x1 = [1,3,5]
y1 = [5, 6, 15]

x2 = ['apples','carrots','bananas','watermelon','cheerios']
##x2 = [1,2,3,4,5]
y2 = [100, 200, 300, 400, 500]

fig, ax = plt.subplots()

ax.scatter(x2, y2)
ax2 = ax.twinx()
ax2.scatter(x1, y1, color = "orange")

plt.show()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190313/3949113f/attachment.html>


More information about the Matplotlib-users mailing list