23

Can I have both twinx and twiny together (i.e. something like twinxy)? I want to put a CDF on a bar plot where the X axis of the bar plot is in log-scale. I cannot make the Ys together, because the bar plot y range is very large comparing [0,1] for CDF.

Any ideas?

Thanks,

Amir
  • 5,996
  • 13
  • 48
  • 61

1 Answers1

37

If I understand your question right, you want to plot two things on the same axes with no shared axis. There is probably a better way to do this, but you can stack twinx (doc) and twiny (doc) as such

ax # your first axes
ax_new = ax.twinx().twiny()

Which will give you tick marks on all sides of the plot. ax will plot against the bottom and left, ax_new will plot against the top and right.

cosmoscalibur
  • 1,131
  • 1
  • 8
  • 17
tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • Seems to be straightforward... I do not know why I failed to test it... Thanks – Amir Sep 10 '12 at 21:18
  • 7
    Great solution, thanks! When using this method (`ax2 = ax1.twinx().twiny()`), I was not able to set the y_label for the second axis, though I could set the x_label. By switching to `ax2 = ax1.twiny().twinx()` I was then able to add the y_label. This, however, raises another issue as I'm unable to turn off the x-tick labels (`ax2.tick_params(axis='x', which='both', top='off', bottom='off', labelbottom='off', labeltop='off')`) as I had been able to before. This method seems to break some of the functionality of the axes and may take some time to work around. – Jason Bellino Jun 26 '14 at 20:35
  • 4
    @Jason That is a funny wrinkle in how the x/ylabels on overlapping axes get handled. You may need to grab the intermediary axes and work on it too. – tacaswell Jun 26 '14 at 21:44