Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib using twinx and twiny together (like twinxy)

Tags:

matplotlib

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,

like image 219
Amir Avatar asked Sep 10 '12 20:09

Amir


People also ask

How do you combine legends in Python?

You can combine the legend items by scooping out the original objects, here via the ax. get_* function to get the labels and the “handles”. You can think of handles as just points/lines/polygons that refer to individual parts of the legend.

How do I make multiple Y-axis in Matplotlib?

Using subplots() method, create a figure and a set of subplots. Plot [1, 2, 3, 4, 5] data points on the left Y-axis scales. Using twinx() method, create a twin of Axes with a shared X-axis but independent Y-axis, ax2. Plot [11, 12, 31, 41, 15] data points on the right Y-axis scale, with blue color.

How do you make twin axis in Python?

Create a new twin Axis sharing the x axis with ax. twinx() : the idea. fig, ax1 = plt. subplots(figsize=(9, 6)) # Instantiate a second axes that shares the same x-axis ax2 = ax1.

How do I create a secondary axis in Matplotlib?

Matplotlib secondary y-axis label We can use ylabel() function or set_ylabel() function to add label at y-axis. In this example, we add a label at the secondary y-axis by using ylabel() method. Import necessary libraries such as numpy and matplotlib. pyplot.


1 Answers

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.

like image 60
tacaswell Avatar answered Oct 01 '22 17:10

tacaswell