Search code examples
pythonmatplotlibwindrose

How to plot a windrose as a subplot


my wind rose diagram going out of the subplot

from windrose import WindroseAxes
fig=plt.figure()
plot1 = plt.subplot2grid((4, 6), (0, 0), rowspan=2, colspan=2)
plot2 = plt.subplot2grid((4, 6), (2, 0), rowspan=2, colspan=2)
plot3 = plt.subplot2grid((4, 6), (0, 2), rowspan=4, colspan=4)

plot1.plot([1,2,3,4],[3,2,5,6])
plot2.scatter([1,3,5,7],[2,7,3,1])
rect = [1, 1, 2, 2]
wa = WindroseAxes(fig, rect)
fig.add_axes(wa)
wa.bar(wd, ws, normed=True, opening=0.8, edgecolor='white')  

final result is this. but i need all three graphs in single figure

I am trying to plot all the 3 graphs in a single figure but I got my wind rose graph out of the figure


Solution

  • Following the example from github, in plt.subplot2grid() and similar functions, you need to set projection='windrose'. You should remove wa = WindroseAxes(fig, rect) and fig.add_axes(wa). At the end of the code plt.tight_layout() seems to work, it moves the subplots a bit to avoid overlapping labels.

    Here is the updated example (also renaming plot1 to ax1, to be more consistent with the matplotlib documentation).

    import matplotlib.pyplot as plt
    import windrose
    import numpy as np
    
    fig = plt.figure()
    ax1 = plt.subplot2grid((4, 6), (0, 0), rowspan=2, colspan=2)
    ax2 = plt.subplot2grid((4, 6), (2, 0), rowspan=2, colspan=2)
    wa = plt.subplot2grid((4, 6), (0, 2), rowspan=4, colspan=4, projection='windrose')
    
    ax1.plot([1, 2, 3, 4], [3, 2, 5, 6])
    ax2.scatter([1, 3, 5, 7], [2, 7, 3, 1])
    ws = np.random.random(500) * 6
    wd = np.random.random(500) * 360
    wa.bar(wd, ws, normed=True, opening=0.8, edgecolor='white')
    plt.tight_layout()
    plt.show()
    

    windrose axes inside a subplot