Skip to content

PySmithPlot/Polar Plot update broken in 2.1.2 (works in 2.0.2) #10654

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

Closed
chrisgmorton opened this issue Mar 3, 2018 · 31 comments · Fixed by #10690
Closed

PySmithPlot/Polar Plot update broken in 2.1.2 (works in 2.0.2) #10654

chrisgmorton opened this issue Mar 3, 2018 · 31 comments · Fixed by #10690
Milestone

Comments

@chrisgmorton
Copy link

chrisgmorton commented Mar 3, 2018

Bug report

Bug summary

The smith chart grid is distorted in 2.1.2, correct in 2.0.2.
Updating the polar plot data blanks the plot in 2.1.2, updates properly in 2.0.2.

In both versions when I create the plots/lines I see:

/Users/chrisgmorton/usr/tcad/fusion/prod/lib/python3.6/site-packages/pysmithplot-0.2.0-py3.6.egg/smithplot/smithhelper.py:46: RuntimeWarning: divide by zero encountered in true_divide
/Users/chrisgmorton/usr/tcad/fusion/prod/lib/python3.6/site-packages/pysmithplot-0.2.0-py3.6.egg/smithplot/smithhelper.py:46: RuntimeWarning: invalid value encountered in true_divide
/Users/chrisgmorton/usr/tcad/fusion/prod/lib/python3.6/site-packages/matplotlib/projections/polar.py:58: RuntimeWarning: invalid value encountered in less
mask = r < 0

On the first update of the plots I see:

Users/chrisgmorton/usr/tcad/fusion/prod/lib/python3.6/site-packages/matplotlib/projections/polar.py:58: RuntimeWarning: invalid value encountered in less
mask = r < 0

and the polar plots are then blanked out (not so with 2.0.2).

Code for reproduction

Plots are built with:

        self.axes=[]
        n = self.numberOfPorts
        
        for i,j in self.indices:
            if i==j: self.axes.append(self.fig.add_subplot(n,n,1+i*2+j, projection='smith'))
            else: self.axes.append(self.fig.add_subplot(n,n,1+i*2+j, projection='polar'))

        for k,(i,j) in enumerate(self.indices):
            self.axes[k].tick_params(which='major', labelsize=10)

        self.fig.subplots_adjust(top=0.9)

Lines initialized with:

        self.lines=[]
        
        for m,(i,j) in enumerate(self.indices):

            if i==j: # Smith Chart
                S=self.Splt.T[j,i].T
                Z = (1+S)/(1-S) # normalized impedance on Smith Chart

                self.lines.append(self.axes[m].plot(Z.T, color='b', marker='', markerfacecolor='b', markeredgecolor='b'))
            else: # Polar Plot
                S = self.Splt.T[j,i].T
                self.lines.append(self.axes[m].plot(np.angle(S.T), np.absolute(S.T), color='r', marker='', markerfacecolor='r', markeredgecolor='r'))

Lines updated with:

        for k,(i,j) in enumerate(self.indices):
            
            if i==j: # Smith Chart
                S=self.Splt.T[j,i].T
                Z = (1+S)/(1-S) # normalized impedance on Smith Chart

                [line.set_xdata(np.real(Z[idx])) for idx, line in enumerate(self.lines[k])]
                [line.set_ydata(np.imag(Z[idx])) for idx, line in enumerate(self.lines[k])]
                
            else: # Polar Plot
                S = self.Splt.T[j,i].T

                [line.set_xdata(np.angle(S[idx])) for idx, line in enumerate(self.lines[k])]
                [line.set_ydata(np.absolute(S[idx])) for idx, line in enumerate(self.lines[k])]

Actual outcome

Matplotlib 2.1.2 output, on completion of simulation:

screen shot 2018-03-02 at 4 59 20 pm

Expected outcome

Matplotlib 2.0.2 output, on completion of simulation:

screen shot 2018-03-02 at 3 18 06 pm

Matplotlib version

  • Operating system: Ubuntu 16.04 and OSX 10.13.3
  • Matplotlib version: 2.1.2
  • Matplotlib backend: Qt5Agg (Qt5.10)
  • Python version: 3.6.4
  • PySmithPlot version: 0.2

Matplotlib installed using pip. Qt5 and PyQt5 built from source. Python 3.6.4 built from source.

@afvincent
Copy link
Contributor

@chrisgmorton Would you have the possibility to test it with the current release candidate (2.2.0rc1), which is available on pip or conda(-forge) ?

@chrisgmorton
Copy link
Author

Will do.

@afvincent
Copy link
Contributor

@chrisgmorton Just in case:

# The rc can be installed via pip
pip install --pre matplotlib

# or via conda from conda-forge
conda install -c conda-forge/label/rc matplotlib -c conda-forge

@chrisgmorton
Copy link
Author

chrisgmorton commented Mar 3, 2018

I used pip3 install matplotlilb==2.2.0rc1 which seemed to work fine.

I'm getting an error with 2.2.0rc1:

AttributeError: module 'matplotlib.cm' has no attribute 'spectral'

I will try and work around to complete the test.

@chrisgmorton
Copy link
Author

chrisgmorton commented Mar 3, 2018

I'm getting the following with this release:

  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/pysmithplot-0.2.0-py3.6.egg/smithplot/smithaxes.py", line 501, in get_yaxis_text1_transform
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 681, in __get__
    instance.majorTicks = [instance._get_tick(major=True)]
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 2161, in _get_tick
    return YTick(self.axes, 0, '', major=major, **tick_kw)
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 186, in __init__
    self.label1 = self._get_text1()
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 568, in _get_text1
    trans, vert, horiz = self._get_text1_transform()
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 545, in _get_text1_transform
    return self.axes.get_yaxis_text1_transform(self._pad)
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/pysmithplot-0.2.0-py3.6.egg/smithplot/smithaxes.py", line 501, in get_yaxis_text1_transform
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 681, in __get__
    instance.majorTicks = [instance._get_tick(major=True)]
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 2161, in _get_tick
    return YTick(self.axes, 0, '', major=major, **tick_kw)
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 186, in __init__
    self.label1 = self._get_text1()
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 568, in _get_text1
    trans, vert, horiz = self._get_text1_transform()
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 545, in _get_text1_transform
    return self.axes.get_yaxis_text1_transform(self._pad)
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/pysmithplot-0.2.0-py3.6.egg/smithplot/smithaxes.py", line 501, in get_yaxis_text1_transform
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 681, in __get__
    instance.majorTicks = [instance._get_tick(major=True)]
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 2161, in _get_tick
    return YTick(self.axes, 0, '', major=major, **tick_kw)
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 186, in __init__
    self.label1 = self._get_text1()
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 568, in _get_text1
    trans, vert, horiz = self._get_text1_transform()
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 545, in _get_text1_transform
    return self.axes.get_yaxis_text1_transform(self._pad)
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/pysmithplot-0.2.0-py3.6.egg/smithplot/smithaxes.py", line 501, in get_yaxis_text1_transform
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 681, in __get__
    instance.majorTicks = [instance._get_tick(major=True)]
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 2161, in _get_tick
    return YTick(self.axes, 0, '', major=major, **tick_kw)
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 182, in __init__
    self.tick1line = self._get_tick1line()
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/axis.py", line 605, in _get_tick1line
    zorder=self._zorder)
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/lines.py", line 401, in __init__
    self._marker = MarkerStyle(marker, fillstyle)
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/markers.py", line 188, in __init__
    self.set_fillstyle(fillstyle)
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/markers.py", line 235, in set_fillstyle
    fillstyle = rcParams['markers.fillstyle']
  File "/Users/chrisgmorton/work/fusion/prod/lib/python3.6/site-packages/matplotlib/__init__.py", line 940, in __getitem__
    val = dict.__getitem__(self, key)
RecursionError: maximum recursion depth exceeded while calling a Python object

@afvincent
Copy link
Contributor

The issue with the spectral color scale is likely to occur because it was deprecated in favor of nipy_spectral. See for example #7315 or #7416

@afvincent
Copy link
Contributor

Would it be possible to have a stand-alone working example that triggers the issue? Without it, it may be hard to track down the source of the problem.

@chrisgmorton
Copy link
Author

I'll try and put something together for you. Will take a little time.

@afvincent
Copy link
Contributor

Attn @QuLogic who fixed a lot of things in polar plots recently :).

@chrisgmorton
Copy link
Author

There are likely two separate issues here. Here's a reproducible case for the first which is specifically that the gridlines for the smith chart produced by pySmithPlot are messed up in matplotlib 2.1.2 vs 2.0.2. To reproduce:

Install pySmithPlot 0.2 from GitHub: https://github.com/vMeijin/pySmithPlot

In the pySmithPlot-master/testbenches directory, run the script smith_short_test.py.

I am using Qt5 so you first will need to set matplotlib's backend to Qt5Agg (I'm not sure that matters for reproducing the issue).

In matplotlib 2.0.2 you see (correct grid):

screen shot 2018-03-05 at 9 17 14 am

In matplotlib 2.1.2 you see (incorrect grid):

screen shot 2018-03-05 at 9 22 41 am

@dstansby
Copy link
Member

dstansby commented Mar 5, 2018

I bet this is an interpolation steps issue... Can you post a self contained example that just plots the gridlines, and doesn't plot any of the data (it can include importing pysmithplot as a dependency)?

@chrisgmorton
Copy link
Author

import matplotlib
matplotlib.use('Qt5Agg')

from matplotlib import pyplot as plt
from smithplot import SmithAxes

fig= plt.figure()
ax = fig.add_subplot(111, projection='smith')
plt.show()

@dstansby
Copy link
Member

dstansby commented Mar 5, 2018

😮 turns out this is my fault! Bisects to 4bc50da

@dstansby
Copy link
Member

dstansby commented Mar 5, 2018

@anntzer running the example above also results in a RecursionError on the master branch, which bisects to c935965

@anntzer
Copy link
Contributor

anntzer commented Mar 5, 2018

Is your commit fixable first, or should I look at mine first?

@anntzer anntzer added this to the v2.2.1 milestone Mar 5, 2018
@dstansby
Copy link
Member

dstansby commented Mar 5, 2018

I think I can fix mine working by working on a commit before yours.

@QuLogic
Copy link
Member

QuLogic commented Mar 5, 2018

PySmithPlot is doing a lot of wrapping of stuff; I have a feeling that a lot of it is provided in the polar changes in 2.0.0. I don't really see anything in the off-diagonal plots that can't be done in regular Matplotlib. edit: I misread the original post and thought all plots were provided by PySmithPlot.

The RecursionError is possibly related to all this wrapping, but I haven't untangled the backtrace yet.

@jklymak
Copy link
Member

jklymak commented Mar 5, 2018

Yes would be preferable to have a non-downstream breaking example a)so we can tell what’s going on and b) so we can add a test.

@dstansby
Copy link
Member

dstansby commented Mar 5, 2018

The only time pySmitPlot calls Arc is

arc = Arc(z_to_xy(zm), d, d, theta1=ang0, theta2=ang1, transform=self._axes.transMoebius)

so I wonder if it's something to do with an Arc with a custom transform set.

@dstansby
Copy link
Member

dstansby commented Mar 5, 2018

Okay @chrisgmorton , some digging has revealed that the problem comes from this line: https://github.com/vMeijin/pySmithPlot/blob/master/smithplot/smithaxes.py#L1254. I suspect you are doing something slightly wrong when manually reversing the order vertices in the path you are manually creating.

Matplotlib handles angles that are the wrong way round okay, so you can just get rid of the 'reverse' case in your code and everything works fine as far as I can tell.

@chrisgmorton
Copy link
Author

I'm not the author of pySmithPlot but I can try removing the reverse cases in the package to see if this resolves my issue. More on the other polar issue shortly.

@chrisgmorton
Copy link
Author

Well, this seems to have solved some of the grid line plotting problems but we still do not have the result shown with matplotlib 2.0.2. This is what the plot looks like with the reverse cases removed:

screen shot 2018-03-05 at 2 21 03 pm

We probably need to get the author to respond. I've opened an issue in the pySmithPlot project. I'll reference this issue there.

@chrisgmorton
Copy link
Author

chrisgmorton commented Mar 6, 2018

Here is the second test case:

import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt

import numpy as np
import numpy.ma as ma

S=np.array([[0.3+0.1j,0.2+0.3j],[0.4-0.5j,-0.3+0.7j],[3.1+0.9j,-0.9-0.3j]])
Splt=ma.array(S)
Splt[:]=ma.masked

fig=plt.figure()
axis=fig.add_subplot(111,projection='polar')
lines=axis.plot(np.angle(Splt.T), np.absolute(Splt.T), marker='x')

[line.set_xdata(np.angle(S[i])) for i,line in enumerate(lines)]
[line.set_ydata(np.absolute(S[i])) for i,line in enumerate(lines)]

axis.set_rlim(np.max(1.1*np.absolute(S)))
axis.legend(np.arange(len(lines)+1),loc=0)

plt.show()

In matplotlib 2.0.2 this works correctly and displays three lines on the polar plot. In Matplotlib 2.1.2 it displays a blank polar plot with no gridlines and no lines. In Matplotlib 2.2 the result is erroneous, the same as with 2.1.2, but also the following messages are displayed in the terminal window:

posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values
posx and posy should be finite values

@dstansby
Copy link
Member

dstansby commented Mar 6, 2018

It seems to work (apart from the radial limits) if set_rlim is commented out, so the problem must be in there somewhere.

@dstansby
Copy link
Member

dstansby commented Mar 6, 2018

If you do set_rlim(0, upper_lim), it works fine. That needs documenting and testing on our side though...

@QuLogic
Copy link
Member

QuLogic commented Mar 6, 2018

So that's mostly not a bug then. If you pass a single value to set_ylim, then it's used for the bottom limit. Previously, set_rlim would only allow passing the top limit and force the bottom to 0. This is no longer the case as negative limits are allowed, and is generally documented in the polar what's new section.

What ends up happening in the end is the bottom limit is greater than the top; I would guess that this should maybe be supported, but as that would invert the axis (as it does in Cartesian coordinates), I suspect that's not what you'd want anyway. @dstansby's fix is the better option for you. You could also do .set_rlim(top=....).

@chrisgmorton
Copy link
Author

Using .set_rlim(top=...) resolves the problem with the polar plots for me. I've made those changes on my side already. I will still need to stick with 2.0.2 until @vMeijin can update pySmithPlot. Thanks.

@dstansby
Copy link
Member

dstansby commented Mar 7, 2018

Great, glad to hear it's fixed!

@vMeijin
Copy link

vMeijin commented Apr 11, 2018

I found the error with the broken Smithchart. It seems that the 'Arc' class I use to create the grid lines was changed. This line was remove

self._path = Path.arc(self.theta1, self.theta2)
When I add it manually in my code it works again. I will soon upload the change to the main branch.

@QuLogic
Copy link
Member

QuLogic commented Apr 12, 2018

@vMeijin a better fix is to remove the reversal, as noted here. It does not appear to be necessary.

@vMeijin
Copy link

vMeijin commented Apr 12, 2018

Removing the reversal does not work. The previous plot already shows that the diagram is still broken with it. The problems seems to be, that the arcs are actually circles due to the removal of the mentioned line of code.

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.

7 participants