Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connectionstyle arc3 with high rad value pushes up data interval of x-axis and y-axis. #19174

Closed
rchee2018 opened this issue Dec 26, 2020 · 2 comments · Fixed by #19214
Closed
Milestone

Comments

@rchee2018
Copy link

I am a hobbyist learning what python can do (and I am enjoying how much python can do!).

When drawing a circular networkx graph, that has arc3 connectionstyle edges, I noticed that the graph "shrank" when I set a high value for rad. Looking deeper, the graph had not shrunk. The x-axis and y-axis had lengthened.

Please refer to the graphs plotted by the code below.

I started with the top-left graph ('low-rad' of 0.5 and not having to set_ylim).

The top-right graph is the graph that 'shrank' when rad was set 'high' (rad=1).

Next, I manually set_ylim for the bottom two graphs.

The bottom-left graph looks like the top-left graph (I estimated the values for the ylim so that they look similar).

The bottom-right graph shows the intended 'high-rad' graph (the edges are high-arched).

Notice the axis ticks of each graph and compare the graphs' x-axis and y-axis data intervals. This suggests that matplotlib treats the 'high-rad' graphs as bigger than how human sees it. If the ticks were not there, I would not have known that the axis had lengthened (networkx switches off the ticks for x-axis and y-axis).

The expected outcome is the graph in the bottom right, without having to set (y-)axis limit (and without axis ticks).

I am using python 3.7.7 and matpotlib 3.2.2.

Thank you.

import networkx as nx
import matplotlib.pyplot as plt

shop_journey = [(1,2,3,4,5,6,7,8,9,10,11,12),(1,7),(7,1)]

G = nx.MultiDiGraph()
for journey in shop_journey:
    if len(journey) > 1:
        nx.add_path(G,journey)

def circle_pattern(ax,connectionstyle,bottom,top):
    pos=nx.circular_layout(G)
    nx.draw_networkx(
        G,
        pos=pos,
        connectionstyle=connectionstyle,
        ax=ax
    )
    ax.axis('equal')
    y1,y2 = tuple(ax.yaxis.get_data_interval())
    x1,x2 = tuple(ax.xaxis.get_data_interval())
    title='{}\nX-axis data interval: {},{}\nY-axis data interval: {},{}\nY-limits: {},{}'.format(
        connectionstyle,
        round(x1,1),round(x2,1),
        round(y1,1),round(y2,1),
        bottom,top
    )
    ax.set_title(label=title)
    ax.set_ylim(bottom,top)
    ax.tick_params(left=True,bottom=True,labelleft=True,labelbottom=True)

fig,axs = plt.subplots(2,2,figsize=(9,9))
circle_pattern(axs[0,0],"arc3,rad=0.5",None,None)
circle_pattern(axs[0,1],"arc3,rad=1",None,None)
circle_pattern(axs[1,0],"arc3,rad=0.5",-1.5,1.5)
circle_pattern(axs[1,1],"arc3,rad=1",-1.5,1.5)
plt.subplots_adjust(hspace=0.9)
plt.show()
@dstansby
Copy link
Member

Thanks for the detailed report! Is it possible to easily modify the example so it only uses Matplotlib (and not networkx)?

@rchee2018
Copy link
Author

My apologies! @dstansby, I admit the example was not simplified enough.

I have looked into the source code of networkx but I am unable to easily modify my first example, to only use matplotlib. So I have attached here a different example to show the same issue.

I looked into the networkx functions in the code I posted above. The source code (networkx.drawing.nx_pylab) calls the FancyArrowPatch from matplotlib.patches. As you know, FancyArrowPatch takes connectionstyle.

Please refer to the graph plotted by the code below. It uses only matplotlib.

Notice how the y-axis lengthens when the curvature of the line increases. The line bulges downwards, but the bottom y-lim decreases so much that the line stays near the 'ceiling' of the plot.

I do not know how the bottom y-lim would reduce so much more than the curve. The y-axis data interval also lengthens so much that it suggests there is 'invisible' data below the curve.

The expected outcome is the line (or curve) stays in the middle of the plot (without having to set y-lim).

Thanks again.

import matplotlib.pyplot as plt
from matplotlib.patches import FancyArrowPatch

def demo(ax,connectionstyle):
    x1,y1 = 0.2,0.4
    x2,y2 = 0.6,0.4
    ax.scatter([x1,x2],[y1,y2])
    arrow = FancyArrowPatch(
        (x1,y1),
        (x2,y2),
        connectionstyle=connectionstyle
        )
    ax.add_patch(arrow)
    ya,yb = tuple(ax.yaxis.get_data_interval())
    xa,xb = tuple(ax.xaxis.get_data_interval())
    title='{}\nX-axis data interval: {},{}\nY-axis data interval: {},{}'.format(
        connectionstyle,
        round(xa,1),round(xb,1),
        round(ya,1),round(yb,1),
    )
    ax.set_title(label=title)
    ax.axis('equal')

fig,axs = plt.subplots(3,1,figsize=(9,9))
demo(axs[0], "arc3,rad=0.0")
demo(axs[1], "arc3,rad=0.5")
demo(axs[2], "arc3,rad=1")
plt.subplots_adjust(hspace=0.8)
plt.show()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants