[Matplotlib-users] plot with 2 scales (not 2 plots)

Neal Becker ndbecker2 at gmail.com
Thu Jan 14 10:15:43 EST 2016


Thanks for the hints.  This one is getting close to what I need, except I
have a scale at the top and the bottom:

from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt

def fahrenheit2celsius(temp):
    """
    Returns temperature in Celsius.
    """
    return temp * 2 / 9


def convert_ax_c_to_celsius(ax_f):
    """
    Update second axis according with first axis.
    """
    y1, y2 = ax_f.get_xlim()
    par2.set_xlim(fahrenheit2celsius(y1), fahrenheit2celsius(y2))
    par2.figure.canvas.draw()

if 1:

    host = host_subplot(111, axes_class=AA.Axes)
    plt.subplots_adjust(bottom=0.25)

#    par1 = host.twinx()
    par2 = host.twiny()

    host.callbacks.connect("xlim_changed", convert_ax_c_to_celsius)

    offset = -45
    new_fixed_axis = par2.get_grid_helper().new_fixed_axis
    par2.axis["bottom"] = new_fixed_axis(loc="bottom",
                                        axes=par2,
                                        offset=(0, offset))

    par2.axis["bottom"].toggle(all=True)

    host.set_xlim(0, 2)
    host.set_ylim(0, 2)

    host.set_xlabel("Distance")
    host.set_ylabel("Density")
    # par1.set_ylabel("Temperature")
    par2.set_xlabel("Velocity")

    p1, = host.plot([0, 1, 2], [0, 1, 2], label="Density")
    # p2, = par1.plot([0, 1, 2], [0, 3, 2], label="Temperature")
    #p3, = par2.plot([0, 1, 2], [50, 30, 15], label="Velocity")

    # par1.set_ylim(0, 4)
    #par2.set_ylim(1, 65)

    host.legend()

    # host.axis["left"].label.set_color(p1.get_color())
    # par1.axis["right"].label.set_color(p2.get_color())
    # par2.axis["right"].label.set_color(p3.get_color())

    plt.draw()
    plt.show()


On Thu, Jan 14, 2016 at 9:54 AM, Benjamin Root <ben.v.root at gmail.com> wrote:

> What did you do for the new_fixed_axis portion of the code? That is the
> key part. If the location isn't set correctly there, the direction will be
> incorrect.
>
> Ben Root
>
> On Thu, Jan 14, 2016 at 9:50 AM, Neal Becker <ndbecker2 at gmail.com> wrote:
>
>> Thanks, but I want 2 scales on the bottom.  The scale produced by twiny
>> seems to go on the top.  While I can fix the position with
>> ax_c = ax_f.twiny()
>> fig.subplots_adjust(bottom=0.2)
>> ax_c.spines["top"].set_position(("axes", -0.2))
>>
>> that will leave me with x-axis label and tick marks going the wrong way
>> for the bottom
>>
>> On Thu, Jan 14, 2016 at 9:43 AM, Benjamin Root <ben.v.root at gmail.com>
>> wrote:
>>
>>> Just do a parasite axis using axisartist. This example does have two
>>> plots, but you don't have to do the first one:
>>> http://matplotlib.org/examples/axes_grid/demo_parasite_axes2.html
>>>
>>> Cheers!
>>> Ben Root
>>>
>>>
>>> On Thu, Jan 14, 2016 at 8:15 AM, Neal Becker <ndbecker2 at gmail.com>
>>> wrote:
>>>
>>>> I'm trying to make a plot which has 2 scales (linearly related), but
>>>> just 1
>>>> set of data.  I've seen many examples with 2 plots and 2 scales, but
>>>> that's
>>>> not what I need here.
>>>>
>>>> I blindly followed some examples and was able to make a pretty good plot
>>>> with one scale on bottom and one on top.  I'd like to see how it looks
>>>> with
>>>> 2 scales on the bottom, with an offset between them, but I don't know
>>>> how to
>>>> do it.
>>>>
>>>> Attached is the current version.
>>>> _______________________________________________
>>>> Matplotlib-users mailing list
>>>> Matplotlib-users at python.org
>>>> https://mail.python.org/mailman/listinfo/matplotlib-users
>>>>
>>>>
>>>
>>
>>
>> --
>> *Those who don't understand recursion are doomed to repeat it*
>>
>
>


-- 
*Those who don't understand recursion are doomed to repeat it*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20160114/06d1abf0/attachment.html>


More information about the Matplotlib-users mailing list