From heiland at iu.edu Tue Jan 2 14:21:58 2018 From: heiland at iu.edu (Heiland, Randy) Date: Tue, 2 Jan 2018 19:21:58 +0000 Subject: [Matplotlib-users] animating scatter points (from files) Message-ID: <13CC052A-36C6-44E8-959A-ADA6245971CE@iu.edu> Hello, I have a use case of wanting to ?animate? scatter points whose coordinates are being written to files (per time step). So, basically, I?d like to be able to loop over reading the files in a directory, parse them to extract coords, and smoothly plot the points in a window. The number of points will change per time step. Suggestions? Do I want to use the FuncAnimation method or something else? thanks! Randy From rmay31 at gmail.com Tue Jan 2 14:41:21 2018 From: rmay31 at gmail.com (Ryan May) Date: Tue, 2 Jan 2018 12:41:21 -0700 Subject: [Matplotlib-users] animating scatter points (from files) In-Reply-To: <13CC052A-36C6-44E8-959A-ADA6245971CE@iu.edu> References: <13CC052A-36C6-44E8-959A-ADA6245971CE@iu.edu> Message-ID: That sounds like a perfect use for FuncAnimation with plot() or scatter() to me. You only really need to use scatter if you want to change the size/color of points based on another variable. Ryan On Tue, Jan 2, 2018 at 12:21 PM, Heiland, Randy wrote: > Hello, > > I have a use case of wanting to ?animate? scatter points whose coordinates > are being written to files (per time step). So, basically, I?d like to be > able to loop over reading the files in a directory, parse them to extract > coords, and smoothly plot the points in a window. The number of points will > change per time step. Suggestions? Do I want to use the FuncAnimation > method or something else? > > thanks! > Randy > > > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users > -- Ryan May -------------- next part -------------- An HTML attachment was scrubbed... URL: From ben.v.root at gmail.com Tue Jan 2 14:50:29 2018 From: ben.v.root at gmail.com (Benjamin Root) Date: Tue, 2 Jan 2018 14:50:29 -0500 Subject: [Matplotlib-users] 3D In-Reply-To: <52B3299A-BE7B-487B-9C9D-B9CD836270EF@netcourrier.com> References: <52B3299A-BE7B-487B-9C9D-B9CD836270EF@netcourrier.com> Message-ID: If you need to export to STL format, nothing in matplotlib will be able to do that, unfortunately. The mplo3d toolkit "fakes" 3D support by squashing everything down to 2D before saving or rendering. There are a bunch of other projects in the python world that might be interesting, but I have no clue if they support STL exports: vispy glumpy mayavi There are probably others, but I can't recall them at the moment. I hope that helps! Ben Root On Mon, Dec 25, 2017 at 5:04 AM, Vincent Douce Mathoscope < mathoscope at netcourrier.com> wrote: > hi > happy christmas ! > i have some projects with 3D impressions and i am wondering which would be > the best solution in order to use my some of my equations still written in > matplotlib > what would you advise me to create some 3D drawings ? i need the > functionality to export my pictures in STL format > thanks > ?????????????????????????? > Vincent Douce > :=: Mathoscope :=: > http://mathoscope.xyz > 06?13?11?07?26 > Bagn?res de Bigorre 65200 > > > > > > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From Nicolas.Rougier at inria.fr Tue Jan 2 15:17:58 2018 From: Nicolas.Rougier at inria.fr (Nicolas Rougier) Date: Tue, 2 Jan 2018 21:17:58 +0100 Subject: [Matplotlib-users] Wind map (sort of) Message-ID: <5E1D0240-8FA9-47AF-B289-4A000106E0EF@inria.fr> Hi all, I?ve made a small animation of stream lines, loosely inspired from https://earth.nullschool.net/; with the help of the streamline script from Raymond Speth. Animation: https://github.com/rougier/windmap/blob/master/windmap.gif Source at: https://github.com/rougier/windmap Nicolas From pmhobson at gmail.com Tue Jan 2 15:39:16 2018 From: pmhobson at gmail.com (Paul Hobson) Date: Tue, 2 Jan 2018 12:39:16 -0800 Subject: [Matplotlib-users] Wind map (sort of) In-Reply-To: <5E1D0240-8FA9-47AF-B289-4A000106E0EF@inria.fr> References: <5E1D0240-8FA9-47AF-B289-4A000106E0EF@inria.fr> Message-ID: Thanks for sharing, Nicolas. This demonstrates several great concepts very elegantly. -p On Tue, Jan 2, 2018 at 12:17 PM, Nicolas Rougier wrote: > > Hi all, > > I?ve made a small animation of stream lines, loosely inspired from > https://earth.nullschool.net/; with the help of the streamline script > from Raymond Speth. > > Animation: https://github.com/rougier/windmap/blob/master/windmap.gif > Source at: https://github.com/rougier/windmap > > > Nicolas > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ben.v.root at gmail.com Tue Jan 2 15:40:37 2018 From: ben.v.root at gmail.com (Benjamin Root) Date: Tue, 2 Jan 2018 15:40:37 -0500 Subject: [Matplotlib-users] Wind map (sort of) In-Reply-To: <5E1D0240-8FA9-47AF-B289-4A000106E0EF@inria.fr> References: <5E1D0240-8FA9-47AF-B289-4A000106E0EF@inria.fr> Message-ID: Ooooh, pretty! This would be nice to include in the gallary! Ben Root On Tue, Jan 2, 2018 at 3:17 PM, Nicolas Rougier wrote: > > Hi all, > > I?ve made a small animation of stream lines, loosely inspired from > https://earth.nullschool.net/; with the help of the streamline script > from Raymond Speth. > > Animation: https://github.com/rougier/windmap/blob/master/windmap.gif > Source at: https://github.com/rougier/windmap > > > Nicolas > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mathoscope at netcourrier.com Tue Jan 2 17:52:07 2018 From: mathoscope at netcourrier.com (Vincent Douce Mathoscope) Date: Tue, 2 Jan 2018 23:52:07 +0100 Subject: [Matplotlib-users] 3D In-Reply-To: References: <52B3299A-BE7B-487B-9C9D-B9CD836270EF@netcourrier.com> Message-ID: thanks Ben i will have a look Blender does not seem to be the right tool because it is really more "bezier/spline" oriented than "math curves" someone on a Blender forum has advised me MathMod that seems ok but not python-compatible anyway it gets out of the subjet of the list Vincent > Le 2 janv. 2018 ? 20:50, Benjamin Root a ?crit : > > If you need to export to STL format, nothing in matplotlib will be able to do that, unfortunately. The mplo3d toolkit "fakes" 3D support by squashing everything down to 2D before saving or rendering. There are a bunch of other projects in the python world that might be interesting, but I have no clue if they support STL exports: > > vispy > glumpy > mayavi > > There are probably others, but I can't recall them at the moment. > > I hope that helps! > Ben Root > > > On Mon, Dec 25, 2017 at 5:04 AM, Vincent Douce Mathoscope > wrote: > hi > happy christmas ! > i have some projects with 3D impressions and i am wondering which would be the best solution in order to use my some of my equations still written in matplotlib > what would you advise me to create some 3D drawings ? i need the functionality to export my pictures in STL format > thanks > ?????????????????????????? > Vincent Douce > :=: Mathoscope :=: > http://mathoscope.xyz > 06?13?11?07?26 > Bagn?res de Bigorre 65200 > > > > > > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users > > ?????????????????????????? Vincent Douce :=: Mathoscope :=: http://mathoscope.xyz 06?13?11?07?26 Bagn?res de Bigorre 65200 -------------- next part -------------- An HTML attachment was scrubbed... URL: From kushal.b15 at iiits.in Fri Jan 5 04:38:40 2018 From: kushal.b15 at iiits.in (KUSHAL BORKAR) Date: Fri, 5 Jan 2018 15:08:40 +0530 Subject: [Matplotlib-users] Project to work on Message-ID: Greetings all, My name is Kushal Borkar. I'm from India and currently I'm pursuing Bachelor's Degree in Electronics and Communication. I have basic knowledge of C++ and Python. I would love to contribute to and learn from 'Matplotlib'. I have done a course on Digital Image Processing and worked on Matplotlib in this course. So, I kindly request to suggest me a project in this field to work upon before the application starts. With Regards, Kushal Borkar. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alexander.bruy at gmail.com Mon Jan 8 11:44:26 2018 From: alexander.bruy at gmail.com (Alexander Bruy) Date: Mon, 8 Jan 2018 18:44:26 +0200 Subject: [Matplotlib-users] Exporting plot with given scale and DPI Message-ID: Hi all. I use matplotlib inside PyQt app and want to export plot into PNG image with some user-defined scale and DPI. So after printing that image I should be able to measure on the paper according to the scale. For example, length of the profile line is 500m (this is my x-axis), scale set to 1:10000 and DPI is 150. 500m at 1:10000 should result in a 5cm print out. Taking 150 DPI into account resulting bitmap should be 295 pixels wide: 1cm is 0.393701 inches 5cm is 1.968505 inches 1.968505inches * 150DPI = 295.27575 px Also it is necessary to take into account that resulting image may be really big, for example if profile line length is several kilometers and DPI is 600. As I can see there is a 'dpi' parameter in the savefig() call, but I can't figure out how to adjust size of the existing figure when exporting. Can you help me? Thanks -- Alexander Bruy From jklymak at uvic.ca Mon Jan 8 12:20:40 2018 From: jklymak at uvic.ca (Jody Klymak) Date: Mon, 8 Jan 2018 09:20:40 -0800 Subject: [Matplotlib-users] Exporting plot with given scale and DPI In-Reply-To: References: Message-ID: <4619C6F2-F3A0-4FCF-BB74-E5BF1B8511C1@uvic.ca> Does `fig.set_size_inches()` do what you want? Cheers, Jody > On 8 Jan 2018, at 08:44, Alexander Bruy wrote: > > Hi all. > > I use matplotlib inside PyQt app and want to export plot into PNG image with > some user-defined scale and DPI. So after printing that image I should be able > to measure on the paper according to the scale. > > For example, length of the profile line is 500m (this is my x-axis), > scale set to 1:10000 and DPI is 150. 500m at 1:10000 should result in > a 5cm print out. Taking 150 DPI into account resulting bitmap should > be 295 pixels wide: > > 1cm is 0.393701 inches > 5cm is 1.968505 inches > 1.968505inches * 150DPI = 295.27575 px > > Also it is necessary to take into account that resulting image may be > really big, for > example if profile line length is several kilometers and DPI is 600. > > As I can see there is a 'dpi' parameter in the savefig() call, but I > can't figure out how > to adjust size of the existing figure when exporting. Can you help me? > > Thanks > -- > Alexander Bruy > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users -- Jody Klymak http://web.uvic.ca/~jklymak/ From paul.deitel at deitel.com Mon Jan 8 12:37:34 2018 From: paul.deitel at deitel.com (Paul Deitel) Date: Mon, 8 Jan 2018 12:37:34 -0500 Subject: [Matplotlib-users] Callback mechanism for animation completed? Message-ID: I am trying to chain together a series of animations. Is there a callback mechanism of some sort that could be used to launch a new animation when another animation completes? From alexander.bruy at gmail.com Mon Jan 8 12:45:43 2018 From: alexander.bruy at gmail.com (Alexander Bruy) Date: Mon, 8 Jan 2018 19:45:43 +0200 Subject: [Matplotlib-users] Exporting plot with given scale and DPI In-Reply-To: <4619C6F2-F3A0-4FCF-BB74-E5BF1B8511C1@uvic.ca> References: <4619C6F2-F3A0-4FCF-BB74-E5BF1B8511C1@uvic.ca> Message-ID: It is not clear from the docs if this function preserves dimensions and DPI when exporting plot into PNG. Also as I understand it resizes plot on screen and does not play well with large sizes. 2018-01-08 19:20 GMT+02:00 Jody Klymak : > Does `fig.set_size_inches()` do what you want? > > Cheers, Jody > >> On 8 Jan 2018, at 08:44, Alexander Bruy wrote: >> >> Hi all. >> >> I use matplotlib inside PyQt app and want to export plot into PNG image with >> some user-defined scale and DPI. So after printing that image I should be able >> to measure on the paper according to the scale. >> >> For example, length of the profile line is 500m (this is my x-axis), >> scale set to 1:10000 and DPI is 150. 500m at 1:10000 should result in >> a 5cm print out. Taking 150 DPI into account resulting bitmap should >> be 295 pixels wide: >> >> 1cm is 0.393701 inches >> 5cm is 1.968505 inches >> 1.968505inches * 150DPI = 295.27575 px >> >> Also it is necessary to take into account that resulting image may be >> really big, for >> example if profile line length is several kilometers and DPI is 600. >> >> As I can see there is a 'dpi' parameter in the savefig() call, but I >> can't figure out how >> to adjust size of the existing figure when exporting. Can you help me? >> >> Thanks >> -- >> Alexander Bruy >> _______________________________________________ >> Matplotlib-users mailing list >> Matplotlib-users at python.org >> https://mail.python.org/mailman/listinfo/matplotlib-users > > -- > Jody Klymak > http://web.uvic.ca/~jklymak/ > > > > > -- Alexander Bruy From efiring at hawaii.edu Mon Jan 8 14:29:44 2018 From: efiring at hawaii.edu (Eric Firing) Date: Mon, 8 Jan 2018 09:29:44 -1000 Subject: [Matplotlib-users] Exporting plot with given scale and DPI In-Reply-To: References: <4619C6F2-F3A0-4FCF-BB74-E5BF1B8511C1@uvic.ca> Message-ID: <2ae2a2b5-59f9-7975-9d86-ad12fd595cb9@hawaii.edu> You are correct that if you are displaying a plot on the screen, the figure size will be changed to fit. This is a shortcoming of all the mpl backends; the plot windows should have automatic scrollbars instead. The workaround is simple, though: When you make the figure, use a small enough dpi so that the window will fit on the screen. Then use your desired dpi when you save: fig, ax = plt.subplots(figsize=(20,30), dpi=20) # ... plot something... fig.savefig('bigplot.png', dpi=150) Assuming you have a typical screen size, the above will result in the following output from the "file" command (in Linux or OSX): bigplot.png: PNG image data, 3000 x 4500, 8-bit/color RGBA, non-interlaced As you see, it is consistent with the requested figsize and file dpi. Eric On 2018/01/08 7:45 AM, Alexander Bruy wrote: > It is not clear from the docs if this function preserves dimensions and DPI > when exporting plot into PNG. > > Also as I understand it resizes plot on screen and does not play well with > large sizes. > > 2018-01-08 19:20 GMT+02:00 Jody Klymak : >> Does `fig.set_size_inches()` do what you want? >> >> Cheers, Jody >> >>> On 8 Jan 2018, at 08:44, Alexander Bruy wrote: >>> >>> Hi all. >>> >>> I use matplotlib inside PyQt app and want to export plot into PNG image with >>> some user-defined scale and DPI. So after printing that image I should be able >>> to measure on the paper according to the scale. >>> >>> For example, length of the profile line is 500m (this is my x-axis), >>> scale set to 1:10000 and DPI is 150. 500m at 1:10000 should result in >>> a 5cm print out. Taking 150 DPI into account resulting bitmap should >>> be 295 pixels wide: >>> >>> 1cm is 0.393701 inches >>> 5cm is 1.968505 inches >>> 1.968505inches * 150DPI = 295.27575 px >>> >>> Also it is necessary to take into account that resulting image may be >>> really big, for >>> example if profile line length is several kilometers and DPI is 600. >>> >>> As I can see there is a 'dpi' parameter in the savefig() call, but I >>> can't figure out how >>> to adjust size of the existing figure when exporting. Can you help me? >>> >>> Thanks >>> -- >>> Alexander Bruy >>> _______________________________________________ >>> Matplotlib-users mailing list >>> Matplotlib-users at python.org >>> https://mail.python.org/mailman/listinfo/matplotlib-users >> >> -- >> Jody Klymak >> http://web.uvic.ca/~jklymak/ >> >> >> >> >> > > > From jdhao at hotmail.com Sun Jan 7 11:16:39 2018 From: jdhao at hotmail.com (Henry east) Date: Sun, 7 Jan 2018 16:16:39 +0000 Subject: [Matplotlib-users] fig.legend method and axes.legend method behaves differently? Message-ID: Hi, I tried to make a plot with two subplots but only one legend since the two subplots express the same set of information. I put the legend outside of the two subplots for better interpretation. I have found that by using fig.legend instead of the ax.legend method. In the saved plot, the legend box are just cut off. I tried to fix this by using bbox_extra_artists argument in the savefig method, following the instruction here. But it does not work. The following code illustrate the problem, import matplotlib.pyplot as plt import numpy as np import matplotlib as mpl x = np.arange(-5, 5, 0.1) y1 = np.sin(x) y2 = np.cos(x) y3 = np.sin(2*x) y4 = np.cos(2*x) fig, (ax1, ax2) = plt.subplots(ncols=1, nrows=2, figsize=(10, 6)) ax1.plot(x, y1, label='sin') ax1.plot(x, y2, label='cos') ax2.plot(x, y3, label='sin') ax2.plot(x, y4, label='cos') handles, labels = ax1.get_legend_handles_labels() # legend = fig.legend(handles, labels, loc='lower left', ncol=2, frameon=False, # bbox_to_anchor=(0.12, 0.88)) # plt.savefig('test.jpg', bbox_extra_artists=(legend,), bbox_inches='tight') legend = ax1.legend(handles, labels, ncol=2, frameon=False, loc='lower left', bbox_to_anchor=(-0.01, 1.0)) plt.savefig('test.jpg', bbox_inches='tight') If I use the above code, the generated image is fine. But if I use the commented code instead, the legend is gone in the generated image. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tcaswell at gmail.com Tue Jan 9 08:15:46 2018 From: tcaswell at gmail.com (Thomas Caswell) Date: Tue, 09 Jan 2018 13:15:46 +0000 Subject: [Matplotlib-users] Callback mechanism for animation completed? In-Reply-To: References: Message-ID: When you say "another animation" do you mean different data on the same artists in the same figure, different artists in the same figure or a different figure altogether? In the first cases I think you could use a generator to sticky tape your data together. In the second two you will have to roll your own. Do you need to be able to change the chain while it is running? If so, what are you using to get your concurrencey? Tom On Mon, Jan 8, 2018, 12:38 Paul Deitel wrote: > I am trying to chain together a series of animations. Is there a callback > mechanism of some sort that could be used to launch a new animation when > another animation completes? > > > > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From paul.deitel at deitel.com Tue Jan 9 11:50:44 2018 From: paul.deitel at deitel.com (Paul Deitel) Date: Tue, 9 Jan 2018 11:50:44 -0500 Subject: [Matplotlib-users] Callback mechanism for animation completed? In-Reply-To: References: Message-ID: <5A062020-F99A-47F5-9A8E-9D48EAD01A1E@deitel.com> Hi Tom, Thanks for your response. Same figure/artists. Basically, for the function called by FuncAnimation, I?d like one of my custom arguments to change after some number of frames. I did not find a good way to do that, so I was thinking I could have one animation that does the first set of frames with the initial custom argument value, followed by another animation that does the remaining frames with a different custom argument value. Thanks, Paul On Jan 9, 2018, at 8:15 AM, Thomas Caswell wrote: When you say "another animation" do you mean different data on the same artists in the same figure, different artists in the same figure or a different figure altogether? In the first cases I think you could use a generator to sticky tape your data together. In the second two you will have to roll your own. Do you need to be able to change the chain while it is running? If so, what are you using to get your concurrencey? Tom On Mon, Jan 8, 2018, 12:38 Paul Deitel > wrote: I am trying to chain together a series of animations. Is there a callback mechanism of some sort that could be used to launch a new animation when another animation completes? _______________________________________________ Matplotlib-users mailing list Matplotlib-users at python.org https://mail.python.org/mailman/listinfo/matplotlib-users -------------- next part -------------- An HTML attachment was scrubbed... URL: From alexander.bruy at gmail.com Tue Jan 9 11:58:41 2018 From: alexander.bruy at gmail.com (Alexander Bruy) Date: Tue, 9 Jan 2018 18:58:41 +0200 Subject: [Matplotlib-users] Exporting plot with given scale and DPI In-Reply-To: <2ae2a2b5-59f9-7975-9d86-ad12fd595cb9@hawaii.edu> References: <4619C6F2-F3A0-4FCF-BB74-E5BF1B8511C1@uvic.ca> <2ae2a2b5-59f9-7975-9d86-ad12fd595cb9@hawaii.edu> Message-ID: Hi, thanks for the hints, I have adopted your suggestions but still have some issues. Now my workflow looks like this: 1. get scale and DPI from user 2. calculate figure width and height in inches based on scale and DPI 3. make a figure with calculated width and height and small DPI value fig = plt.figure(figsize=(imgWidth, imgWidth), dpi=10) 4. plot profile line 5. export figure with correct DPI fig.savefig(fileName, dpi=int(userDPI), format="png", bbox_inches="tight") Unfortunately, when I export figure with correct DPI I get incorrect image dimensions. After some experiments, I have found that adding fig.show() *before* the savefig() call fixes things and resulting image is correct. Here is the image obtained without fig.show() for example https://ibb.co/dx03E6 And here is correct result I get with fig.show() added before savefig() https://ibb.co/gZ0ygm Any ideas what can be wrong? I tried to remove bbox_inches="tight" but still no luck, output is not correct anyway. Using mpl.use('Agg') or similar things is not possible as matplotlib embeeded into Qt application. I can live with fig.show() workaround, but need a way to close plot window programmaticaly after saving the figure. 2018-01-08 21:29 GMT+02:00 Eric Firing : > You are correct that if you are displaying a plot on the screen, the figure > size will be changed to fit. This is a shortcoming of all the mpl backends; > the plot windows should have automatic scrollbars instead. The workaround is > simple, though: When you make the figure, use a small enough dpi so that the > window will fit on the screen. Then use your desired dpi when you save: > > fig, ax = plt.subplots(figsize=(20,30), dpi=20) > # ... plot something... > fig.savefig('bigplot.png', dpi=150) > > Assuming you have a typical screen size, the above will result in the > following output from the "file" command (in Linux or OSX): > > bigplot.png: PNG image data, 3000 x 4500, 8-bit/color RGBA, non-interlaced > > As you see, it is consistent with the requested figsize and file dpi. > > Eric > > > On 2018/01/08 7:45 AM, Alexander Bruy wrote: >> >> It is not clear from the docs if this function preserves dimensions and >> DPI >> when exporting plot into PNG. >> >> Also as I understand it resizes plot on screen and does not play well with >> large sizes. >> >> 2018-01-08 19:20 GMT+02:00 Jody Klymak : >>> >>> Does `fig.set_size_inches()` do what you want? >>> >>> Cheers, Jody >>> >>>> On 8 Jan 2018, at 08:44, Alexander Bruy >>>> wrote: >>>> >>>> Hi all. >>>> >>>> I use matplotlib inside PyQt app and want to export plot into PNG image >>>> with >>>> some user-defined scale and DPI. So after printing that image I should >>>> be able >>>> to measure on the paper according to the scale. >>>> >>>> For example, length of the profile line is 500m (this is my x-axis), >>>> scale set to 1:10000 and DPI is 150. 500m at 1:10000 should result in >>>> a 5cm print out. Taking 150 DPI into account resulting bitmap should >>>> be 295 pixels wide: >>>> >>>> 1cm is 0.393701 inches >>>> 5cm is 1.968505 inches >>>> 1.968505inches * 150DPI = 295.27575 px >>>> >>>> Also it is necessary to take into account that resulting image may be >>>> really big, for >>>> example if profile line length is several kilometers and DPI is 600. >>>> >>>> As I can see there is a 'dpi' parameter in the savefig() call, but I >>>> can't figure out how >>>> to adjust size of the existing figure when exporting. Can you help me? >>>> >>>> Thanks >>>> -- >>>> Alexander Bruy >>>> _______________________________________________ >>>> Matplotlib-users mailing list >>>> Matplotlib-users at python.org >>>> https://mail.python.org/mailman/listinfo/matplotlib-users >>> >>> >>> -- >>> Jody Klymak >>> http://web.uvic.ca/~jklymak/ >>> >>> >>> >>> >>> >> >> >> > > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users -- Alexander Bruy From efiring at hawaii.edu Wed Jan 10 11:46:10 2018 From: efiring at hawaii.edu (Eric Firing) Date: Wed, 10 Jan 2018 06:46:10 -1000 Subject: [Matplotlib-users] Exporting plot with given scale and DPI In-Reply-To: References: <4619C6F2-F3A0-4FCF-BB74-E5BF1B8511C1@uvic.ca> <2ae2a2b5-59f9-7975-9d86-ad12fd595cb9@hawaii.edu> Message-ID: <1c4cdeab-23fd-55d3-c564-028872f6f078@hawaii.edu> It looks like somewhere you are setting an aspect ratio of 1 (or 'equal') and the difference between the examples is that the first is with adjustable 'box' and the second is with adjustable 'datalim'. Are you doing any sharing or twinning of axes? In place of the call to show, try putting in ax.set_aspect('datalim') ax.apply_aspect() Eric On 2018/01/09 6:58 AM, Alexander Bruy wrote: > Hi, > > thanks for the hints, I have adopted your suggestions but still have > some issues. > Now my workflow looks like this: > 1. get scale and DPI from user > 2. calculate figure width and height in inches based on scale and DPI > 3. make a figure with calculated width and height and small DPI value > fig = plt.figure(figsize=(imgWidth, imgWidth), dpi=10) > 4. plot profile line > 5. export figure with correct DPI > fig.savefig(fileName, dpi=int(userDPI), format="png", bbox_inches="tight") > > Unfortunately, when I export figure with correct DPI I get incorrect image > dimensions. After some experiments, I have found that adding fig.show() > *before* the savefig() call fixes things and resulting image is correct. > > Here is the image obtained without fig.show() > for example https://ibb.co/dx03E6 > > And here is correct result I get with fig.show() added before savefig() > https://ibb.co/gZ0ygm > > Any ideas what can be wrong? I tried to remove bbox_inches="tight" but > still no luck, output is not correct anyway. Using mpl.use('Agg') or similar > things is not possible as matplotlib embeeded into Qt application. > > I can live with fig.show() workaround, but need a way to close plot window > programmaticaly after saving the figure. > > 2018-01-08 21:29 GMT+02:00 Eric Firing : >> You are correct that if you are displaying a plot on the screen, the figure >> size will be changed to fit. This is a shortcoming of all the mpl backends; >> the plot windows should have automatic scrollbars instead. The workaround is >> simple, though: When you make the figure, use a small enough dpi so that the >> window will fit on the screen. Then use your desired dpi when you save: >> >> fig, ax = plt.subplots(figsize=(20,30), dpi=20) >> # ... plot something... >> fig.savefig('bigplot.png', dpi=150) >> >> Assuming you have a typical screen size, the above will result in the >> following output from the "file" command (in Linux or OSX): >> >> bigplot.png: PNG image data, 3000 x 4500, 8-bit/color RGBA, non-interlaced >> >> As you see, it is consistent with the requested figsize and file dpi. >> >> Eric >> >> >> On 2018/01/08 7:45 AM, Alexander Bruy wrote: >>> >>> It is not clear from the docs if this function preserves dimensions and >>> DPI >>> when exporting plot into PNG. >>> >>> Also as I understand it resizes plot on screen and does not play well with >>> large sizes. >>> >>> 2018-01-08 19:20 GMT+02:00 Jody Klymak : >>>> >>>> Does `fig.set_size_inches()` do what you want? >>>> >>>> Cheers, Jody >>>> >>>>> On 8 Jan 2018, at 08:44, Alexander Bruy >>>>> wrote: >>>>> >>>>> Hi all. >>>>> >>>>> I use matplotlib inside PyQt app and want to export plot into PNG image >>>>> with >>>>> some user-defined scale and DPI. So after printing that image I should >>>>> be able >>>>> to measure on the paper according to the scale. >>>>> >>>>> For example, length of the profile line is 500m (this is my x-axis), >>>>> scale set to 1:10000 and DPI is 150. 500m at 1:10000 should result in >>>>> a 5cm print out. Taking 150 DPI into account resulting bitmap should >>>>> be 295 pixels wide: >>>>> >>>>> 1cm is 0.393701 inches >>>>> 5cm is 1.968505 inches >>>>> 1.968505inches * 150DPI = 295.27575 px >>>>> >>>>> Also it is necessary to take into account that resulting image may be >>>>> really big, for >>>>> example if profile line length is several kilometers and DPI is 600. >>>>> >>>>> As I can see there is a 'dpi' parameter in the savefig() call, but I >>>>> can't figure out how >>>>> to adjust size of the existing figure when exporting. Can you help me? >>>>> >>>>> Thanks >>>>> -- >>>>> Alexander Bruy >>>>> _______________________________________________ >>>>> Matplotlib-users mailing list >>>>> Matplotlib-users at python.org >>>>> https://mail.python.org/mailman/listinfo/matplotlib-users >>>> >>>> >>>> -- >>>> Jody Klymak >>>> http://web.uvic.ca/~jklymak/ >>>> >>>> >>>> >>>> >>>> >>> >>> >>> >> >> _______________________________________________ >> Matplotlib-users mailing list >> Matplotlib-users at python.org >> https://mail.python.org/mailman/listinfo/matplotlib-users > > > From alexander.bruy at gmail.com Wed Jan 10 12:58:53 2018 From: alexander.bruy at gmail.com (Alexander Bruy) Date: Wed, 10 Jan 2018 19:58:53 +0200 Subject: [Matplotlib-users] Exporting plot with given scale and DPI In-Reply-To: <1c4cdeab-23fd-55d3-c564-028872f6f078@hawaii.edu> References: <4619C6F2-F3A0-4FCF-BB74-E5BF1B8511C1@uvic.ca> <2ae2a2b5-59f9-7975-9d86-ad12fd595cb9@hawaii.edu> <1c4cdeab-23fd-55d3-c564-028872f6f078@hawaii.edu> Message-ID: Hi Eric, 2018-01-10 18:46 GMT+02:00 Eric Firing : > It looks like somewhere you are setting an aspect ratio of 1 (or 'equal') > and the difference between the examples is that the first is with adjustable > 'box' and the second is with adjustable 'datalim'. Right, I create axes using following code: axes = fig.add_axes((LEFT_MARGIN, BOTTOM_MARGIN, RIGHT_MARGIN, TOP_MARGIN), adjustable="datalim", aspect=1) > Are you doing any sharing or twinning of axes? No sharing or twinning, just single line and one pair (X and Y) of axes. -- Alexander Bruy From jklymak at uvic.ca Wed Jan 10 13:16:42 2018 From: jklymak at uvic.ca (Jody Klymak) Date: Wed, 10 Jan 2018 10:16:42 -0800 Subject: [Matplotlib-users] Exporting plot with given scale and DPI In-Reply-To: References: <4619C6F2-F3A0-4FCF-BB74-E5BF1B8511C1@uvic.ca> <2ae2a2b5-59f9-7975-9d86-ad12fd595cb9@hawaii.edu> <1c4cdeab-23fd-55d3-c564-028872f6f078@hawaii.edu> Message-ID: <2153CC46-3B31-4925-AEA2-6CAD79138BAC@uvic.ca> Can you give us a minimal self-contained working example of exactly what you are trying to do? Cheers, Jody > On 10 Jan 2018, at 09:58, Alexander Bruy wrote: > > Hi Eric, > > 2018-01-10 18:46 GMT+02:00 Eric Firing : >> It looks like somewhere you are setting an aspect ratio of 1 (or 'equal') >> and the difference between the examples is that the first is with adjustable >> 'box' and the second is with adjustable 'datalim'. > > Right, I create axes using following code: > > axes = fig.add_axes((LEFT_MARGIN, BOTTOM_MARGIN, RIGHT_MARGIN, TOP_MARGIN), > adjustable="datalim", aspect=1) > >> Are you doing any sharing or twinning of axes? > > No sharing or twinning, just single line and one pair (X and Y) of axes. > > -- > Alexander Bruy > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users -- Jody Klymak http://web.uvic.ca/~jklymak/ From cmr.pent at gmail.com Thu Jan 11 01:13:36 2018 From: cmr.pent at gmail.com (=?UTF-8?B?0JDQvdC00YDQtdC5INCf0LDRgNCw0LzQvtC90L7Qsg==?=) Date: Thu, 11 Jan 2018 09:13:36 +0300 Subject: [Matplotlib-users] PowerNorm runtime warning Message-ID: Hello! When running import matplotlib.colors as colors cmap = colors.PowerNorm(vmin = -1, vmax = 1, gamma = 3) print(cmap(-1)) print(cmap(+0)) print(cmap(+1)) I expectedly get 0.0 0.125 1.0 However PowerNorm.autoscale* have if self.vmin < 0: self.vmin = 0 warnings.warn("Power-law scaling on negative values is ill-defined, clamping to 0.") Why is it so? PowerNorm does work correctly for negative vmin! Best wishes, Andrey Paramonov -------------- next part -------------- An HTML attachment was scrubbed... URL: From ndbecker2 at gmail.com Thu Jan 11 08:33:21 2018 From: ndbecker2 at gmail.com (Neal Becker) Date: Thu, 11 Jan 2018 08:33:21 -0500 Subject: [Matplotlib-users] reset color cycler? Message-ID: Some answers were given here: https://stackoverflow.com/questions/24193174/reset-color-cycle-in-matplotlib I prefer the ax.set_prop_cycler(None) approach, but I want to reset only the 'color' (I assume this will reset all cyclers to defaults). I would have expected something like this to work: ax.set_prop_cycler('color', None) but it doesn't. From ben.v.root at gmail.com Thu Jan 11 11:44:47 2018 From: ben.v.root at gmail.com (Benjamin Root) Date: Thu, 11 Jan 2018 11:44:47 -0500 Subject: [Matplotlib-users] reset color cycler? In-Reply-To: References: Message-ID: That is not an available feature in the underlying cycler class. We would need to implement a new feature in cycler first, then add our hooks to it in matplotlib. On Thu, Jan 11, 2018 at 8:33 AM, Neal Becker wrote: > Some answers were given here: > > https://stackoverflow.com/questions/24193174/reset- > color-cycle-in-matplotlib > > I prefer the > > ax.set_prop_cycler(None) > > approach, but I want to reset only the 'color' (I assume this will reset > all > cyclers to defaults). I would have expected something like this to work: > > ax.set_prop_cycler('color', None) > > but it doesn't. > > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kotaro.saito at psi.ch Thu Jan 11 12:53:21 2018 From: kotaro.saito at psi.ch (Saito Kotaro (PSI)) Date: Thu, 11 Jan 2018 18:53:21 +0100 Subject: [Matplotlib-users] reset color cycler? Message-ID: <6448CF52-3D03-47C4-979E-24311A5A8ECC@psi.ch> Hi Neal, I?m not sure about your purpose for resetting the colour cycle, but this answer might be helpful. https://stackoverflow.com/a/47773515/9131000 Best regards, Kotaro From kotaro.saito at psi.ch Thu Jan 11 17:51:50 2018 From: kotaro.saito at psi.ch (Saito Kotaro (PSI)) Date: Thu, 11 Jan 2018 23:51:50 +0100 Subject: [Matplotlib-users] reset color cycler? In-Reply-To: References: <6448CF52-3D03-47C4-979E-24311A5A8ECC@psi.ch> Message-ID: <142F8ECC-F5F3-4984-B49E-D2007592139D@psi.ch> Backend doesn?t matter in your case. I recommend to apply 'Colors from color cycle? method in the SO answer. The idea is that, first, you get a list of colors from your color cycle before making a pdf page. Then you plot your data with color keyword, like plot(x, y, color=color_list_from_colorcycle[i]) with an index number ?i? incremented after each plot, save a pdf page, reset the index ?i? to zero, repeat the procedure whatever times you want. It?s not elegant at all but I?m sure it solves the problem. If first ten colors in your color cycle is enough for you, then ?CN notation? method is also applicable. (matplotlib 2 is required for this method) Best, Kotaro > 2018/01/11 19:08?Neal Becker ????: > > I'm using PdfPages to produces multiple plots. At the start of each page I reset the color cycle. > > On Thu, Jan 11, 2018 at 12:55 PM Saito Kotaro (PSI) wrote: > Hi Neal, > > I?m not sure about your purpose for resetting the colour cycle, but this answer might be helpful. > > https://stackoverflow.com/a/47773515/9131000 > > Best regards, > > Kotaro > From efiring at hawaii.edu Fri Jan 12 01:59:16 2018 From: efiring at hawaii.edu (Eric Firing) Date: Thu, 11 Jan 2018 20:59:16 -1000 Subject: [Matplotlib-users] Project to work on In-Reply-To: References: Message-ID: Thank you for your expression of interest. We welcome new contributors! I don't have a specific project to suggest, and it would be difficult to do so without more familiarity with your skills and interests. Usually people begin contributing to Matplotlib gradually, with smaller fixes (documentation and/or code) and review of existing PRs. We have distressingly many open issues and PRs--have a look, see if some of them match your interests and skills. If you haven't already done so, check out https://matplotlib.org/devel/index.html. Eric On 2018/01/04 11:38 PM, KUSHAL BORKAR wrote: > Greetings all, > ????????????????????? My name isKushalBorkar. I'm from India and > currently I'm pursuing Bachelor's Degree in Electronics and > Communication. I have basic knowledge of C++ and Python. I would love to > contribute to and learn from 'Matplotlib'. I have done a course on > Digital Image Processing and worked onMatplotlibin this course. > ???????????????????? So, I kindly request to suggest me a project in > this field to work upon before the application starts. > > With Regards, > KushalBorkar. > > > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users > From efiring at hawaii.edu Fri Jan 12 02:31:11 2018 From: efiring at hawaii.edu (Eric Firing) Date: Thu, 11 Jan 2018 21:31:11 -1000 Subject: [Matplotlib-users] fig.legend method and axes.legend method behaves differently? In-Reply-To: References: Message-ID: <27163d47-f9e4-f779-2cc2-df5bf3904348@hawaii.edu> It looks like the bbox_to_anchor argument is causing the problem. If you try this: legend = fig.legend(handles, labels, loc='upper center', ncol=2, frameon=False,) plt.savefig('test.jpg', bbox_extra_artists=(legend,), bbox_inches='tight') it works, with 'upper center' or 'lower center'. Eric On 2018/01/07 6:16 AM, Henry east wrote: > Hi, > I tried to make a plot with two subplots but only one legend since the > two subplots express the same set of information. > I put the legend outside of the two subplots for better interpretation. > I have found that by using |fig.legend| instead of > the |ax.legend| method. In the saved plot, the legend box are just cut off. > > I tried to fix this by using |bbox_extra_artists| argument in the > |savefig| method, following the instruction > here > . > But it does not work. The following code illustrate the problem, From rory.yorke at gmail.com Sat Jan 20 14:44:18 2018 From: rory.yorke at gmail.com (Rory Yorke) Date: Sat, 20 Jan 2018 21:44:18 +0200 Subject: [Matplotlib-users] Replacing deprecated use of pyplot.subplot Message-ID: <87shb0mi25.fsf@gmail.com> Hi, I'm a contributor to the Python Control Systems Library [1], which uses Matplotlib for plotting. We recently noticed deprecation warnings due to how we use pyplot.subplot. We use it in the Matlab manner of either getting a handle to an existing axis, or creating one if no suitable axis exists. The warning is MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance. For example, to plot the frequency response a linear dynamical system (AKA Bode plot of the system), the relevant function could be simplified to: def bode_plot(g): freq, mag, phase = freq_resp(g) subplot(211) semilogx(freq, 20*log10(mag)) subplot(212) semilogx(freq, phase) We've replaced that with code like this: def bode_plot(g): freq, mag, phase = freq_resp(g) ax_mag = None ax_phase = None for ax in gcf.axes(): if ax.get_label() == 'control-bode-magnitude': ax_mag = ax elif ax.get_label() == 'control-bode-phase': ax_phase = ax if ax_mag is None or ax_phase is None: clf() ax_mag = subplot(211, label = 'control-bode-magnitude') ax_phase = subplot(212, label = 'control-bode-phase) ax_mag.semilogx(freq, 20*log10(mag)) ax_phase.semilogx(freq, phase) This means that calls like bode_plot(g) bode_plot(h) will show the response of g and h on the same figure. Is this method of using labels to check for existing axes reasonable? Is there a better way? Actual code exhibiting warnings at [2]; new code at [3]. The latter link is to an as-yet unmerged branch, and may disappear. Thanks, Rory [1] https://github.com/python-control/python-control [2] https://github.com/python-control/python-control/blob/af8d4ee39dfa574c2b3b335f4cdb4be858ae469a/control/freqplot.py#L175 [3] https://github.com/murrayrm/python-control/blob/dc1820a4e64d73937c7de8df078c41ec1773e048/control/freqplot.py#L138 From pmhobson at gmail.com Sat Jan 20 16:02:02 2018 From: pmhobson at gmail.com (Paul Hobson) Date: Sat, 20 Jan 2018 13:02:02 -0800 Subject: [Matplotlib-users] Replacing deprecated use of pyplot.subplot In-Reply-To: <87shb0mi25.fsf@gmail.com> References: <87shb0mi25.fsf@gmail.com> Message-ID: Hey Rory, In general, especially for library code, you should avoid relying on the pyplot state machine. That means explicitly passing Axes and Figure object around to and from your functions. For me, making that switch meant added an `ax=None` kwarg to the end of my function signatures. Then I carry around an axes_validator function that looks something like this: https://github.com/matplotlib/mpl-probscale/blob/master/probscale/validate.py#L6 But that's not really necessary. So in your case, I think you should do something like this: import numpy as np import matplotlib.pyplot as plt def bode_plot(g, ax_mag=None, ax_phase=None, mag_opts=None, phase_opts=None): # create axes if they're not both supplied if not ax_mag or not ax_phase: fig, (ax_mag, ax_phase) = plt.subplots(nrows=2) # make the plotting options empty dicts if # not supplied if not mag_opts: mag_opts = {} if not phase_opts: phase_opts = {} # compute signal stuff freq, mag, phase = freq_resp(g) # 99% sure semilogx returns a tuple of artists, so I unpack it # you should check this though mag_artist, = ax_mag.semilogx(freq, 20 * np.log10(mag), **mag_opts) phase_artist, = ax_phase.semilogx(freq, phase, **phase_opts) # package the output for later (if you want to modify artists) output = { 'fig': fig, 'axes': (ax_mag, ax_phase), 'arists': (mag_artist, phase_artist) } return output And then you'd use the code like this: fig, (ax_mag, ax_phase) = plt.subplots(nrows=2, figsize=(12, 6)) g_mpl = bode_plot(g, ax_mag=ax_mag, ax_phase=ax_phase, color='r', linewidth=2, label='G') h_mpl = bode_plot(h, ax_mag=ax_mag, ax_phase=ax_phase, color='b'm linewidth=1, label='H') ax_mag.legend() Or do something like this: g_mpl = bode_plot(g, color='r', linewidth=2, label='G') h_mpl = bode_plot(h, ax_mag=g_mpl['axes'][0], ax_phase=g_mpl['axes'][0]) Does that help? -Paul On Sat, Jan 20, 2018 at 11:44 AM, Rory Yorke wrote: > Hi, > > I'm a contributor to the Python Control Systems Library [1], which uses > Matplotlib for plotting. > > We recently noticed deprecation warnings due to how we use > pyplot.subplot. We use it in the Matlab manner of either getting a > handle to an existing axis, or creating one if no suitable axis exists. > > The warning is > > MatplotlibDeprecationWarning: Adding an axes using the same arguments > as a previous axes currently reuses the earlier instance. In a future > version, a new instance will always be created and returned. > Meanwhile, this warning can be suppressed, and the future behavior > ensured, by passing a unique label to each axes instance. > > For example, to plot the frequency response a linear dynamical system > (AKA Bode plot of the system), the relevant function could be simplified > to: > > def bode_plot(g): > freq, mag, phase = freq_resp(g) > subplot(211) > semilogx(freq, 20*log10(mag)) > subplot(212) > semilogx(freq, phase) > > We've replaced that with code like this: > > def bode_plot(g): > freq, mag, phase = freq_resp(g) > > ax_mag = None > ax_phase = None > for ax in gcf.axes(): > if ax.get_label() == 'control-bode-magnitude': > ax_mag = ax > elif ax.get_label() == 'control-bode-phase': > ax_phase = ax > > if ax_mag is None or ax_phase is None: > clf() > ax_mag = subplot(211, label = 'control-bode-magnitude') > ax_phase = subplot(212, label = 'control-bode-phase) > > ax_mag.semilogx(freq, 20*log10(mag)) > ax_phase.semilogx(freq, phase) > > This means that calls like > > bode_plot(g) > bode_plot(h) > > will show the response of g and h on the same figure. > > Is this method of using labels to check for existing axes reasonable? > Is there a better way? > > Actual code exhibiting warnings at [2]; new code at [3]. The latter > link is to an as-yet unmerged branch, and may disappear. > > Thanks, > > Rory > > [1] https://github.com/python-control/python-control > [2] https://github.com/python-control/python-control/blob/ > af8d4ee39dfa574c2b3b335f4cdb4be858ae469a/control/freqplot.py#L175 > [3] https://github.com/murrayrm/python-control/blob/ > dc1820a4e64d73937c7de8df078c41ec1773e048/control/freqplot.py#L138 > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From pmhobson at gmail.com Sat Jan 20 16:03:38 2018 From: pmhobson at gmail.com (Paul Hobson) Date: Sat, 20 Jan 2018 13:03:38 -0800 Subject: [Matplotlib-users] Replacing deprecated use of pyplot.subplot In-Reply-To: References: <87shb0mi25.fsf@gmail.com> Message-ID: Errr, make that last line: h_mpl = bode_plot(h, ax_mag=g_mpl['axes'][0], ax_phase=g_mpl['axes']*[1]*) On Sat, Jan 20, 2018 at 1:02 PM, Paul Hobson wrote: > Hey Rory, > > In general, especially for library code, you should avoid relying on the > pyplot state machine. > > That means explicitly passing Axes and Figure object around to and from > your functions. > > For me, making that switch meant added an `ax=None` kwarg to the end of my > function signatures. > > Then I carry around an axes_validator function that looks something like > this: > https://github.com/matplotlib/mpl-probscale/blob/master/ > probscale/validate.py#L6 > > But that's not really necessary. So in your case, I think you should do > something like this: > > > import numpy as np > import matplotlib.pyplot as plt > > > def bode_plot(g, ax_mag=None, ax_phase=None, mag_opts=None, > phase_opts=None): > # create axes if they're not both supplied > if not ax_mag or not ax_phase: > fig, (ax_mag, ax_phase) = plt.subplots(nrows=2) > > # make the plotting options empty dicts if > # not supplied > if not mag_opts: > mag_opts = {} > > if not phase_opts: > phase_opts = {} > > # compute signal stuff > freq, mag, phase = freq_resp(g) > > # 99% sure semilogx returns a tuple of artists, so I unpack it > # you should check this though > mag_artist, = ax_mag.semilogx(freq, 20 * np.log10(mag), **mag_opts) > phase_artist, = ax_phase.semilogx(freq, phase, **phase_opts) > > # package the output for later (if you want to modify artists) > output = { > 'fig': fig, > 'axes': (ax_mag, ax_phase), > 'arists': (mag_artist, phase_artist) > } > return output > > And then you'd use the code like this: > > fig, (ax_mag, ax_phase) = plt.subplots(nrows=2, figsize=(12, 6)) > g_mpl = bode_plot(g, ax_mag=ax_mag, ax_phase=ax_phase, color='r', > linewidth=2, label='G') > h_mpl = bode_plot(h, ax_mag=ax_mag, ax_phase=ax_phase, color='b'm > linewidth=1, label='H') > ax_mag.legend() > > > Or do something like this: > g_mpl = bode_plot(g, color='r', linewidth=2, label='G') > h_mpl = bode_plot(h, ax_mag=g_mpl['axes'][0], ax_phase=g_mpl['axes'][0]) > > > Does that help? > -Paul > > > On Sat, Jan 20, 2018 at 11:44 AM, Rory Yorke wrote: > >> Hi, >> >> I'm a contributor to the Python Control Systems Library [1], which uses >> Matplotlib for plotting. >> >> We recently noticed deprecation warnings due to how we use >> pyplot.subplot. We use it in the Matlab manner of either getting a >> handle to an existing axis, or creating one if no suitable axis exists. >> >> The warning is >> >> MatplotlibDeprecationWarning: Adding an axes using the same arguments >> as a previous axes currently reuses the earlier instance. In a future >> version, a new instance will always be created and returned. >> Meanwhile, this warning can be suppressed, and the future behavior >> ensured, by passing a unique label to each axes instance. >> >> For example, to plot the frequency response a linear dynamical system >> (AKA Bode plot of the system), the relevant function could be simplified >> to: >> >> def bode_plot(g): >> freq, mag, phase = freq_resp(g) >> subplot(211) >> semilogx(freq, 20*log10(mag)) >> subplot(212) >> semilogx(freq, phase) >> >> We've replaced that with code like this: >> >> def bode_plot(g): >> freq, mag, phase = freq_resp(g) >> >> ax_mag = None >> ax_phase = None >> for ax in gcf.axes(): >> if ax.get_label() == 'control-bode-magnitude': >> ax_mag = ax >> elif ax.get_label() == 'control-bode-phase': >> ax_phase = ax >> >> if ax_mag is None or ax_phase is None: >> clf() >> ax_mag = subplot(211, label = 'control-bode-magnitude') >> ax_phase = subplot(212, label = 'control-bode-phase) >> >> ax_mag.semilogx(freq, 20*log10(mag)) >> ax_phase.semilogx(freq, phase) >> >> This means that calls like >> >> bode_plot(g) >> bode_plot(h) >> >> will show the response of g and h on the same figure. >> >> Is this method of using labels to check for existing axes reasonable? >> Is there a better way? >> >> Actual code exhibiting warnings at [2]; new code at [3]. The latter >> link is to an as-yet unmerged branch, and may disappear. >> >> Thanks, >> >> Rory >> >> [1] https://github.com/python-control/python-control >> [2] https://github.com/python-control/python-control/blob/af8d4e >> e39dfa574c2b3b335f4cdb4be858ae469a/control/freqplot.py#L175 >> [3] https://github.com/murrayrm/python-control/blob/dc1820a4e64d >> 73937c7de8df078c41ec1773e048/control/freqplot.py#L138 >> _______________________________________________ >> Matplotlib-users mailing list >> Matplotlib-users at python.org >> https://mail.python.org/mailman/listinfo/matplotlib-users >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From efiring at hawaii.edu Sat Jan 20 16:52:37 2018 From: efiring at hawaii.edu (Eric Firing) Date: Sat, 20 Jan 2018 11:52:37 -1000 Subject: [Matplotlib-users] Replacing deprecated use of pyplot.subplot In-Reply-To: <87shb0mi25.fsf@gmail.com> References: <87shb0mi25.fsf@gmail.com> Message-ID: <0d28481b-2e01-1981-f5e4-6e1dfede4448@hawaii.edu> Rory, The general direction of Matplotlib's evolution is toward encouraging more explicit code, so that the programmer or user has more responsibility for specifying which figure and axes are to be used, rather than relying on the state machine and the concepts of "current figure" and "current axes". Therefore we recommend using pyplot functions very sparingly. If you need to keep your API exactly as it is, your approach using labels looks reasonable. I think it can be simplified, though, by defining a helper function something like this: def _get_bode_axes(): fig = plt.gcf() if not hasattr(fig, '_bode_axes'): fig.clf() fig._bode_axes = fig.subplots(2, 1, sharex=True) return fig._bode_axes Then, outside any loop in plot_bode but conditional on the Plot kwarg, use a single call: ax_mag, ax_phase = _get_bode_axes() Also conditional on Plot, put your loop over syslist to do the plotting. I would make that loop separate from the calculation. In general, code is clearer and easier to test when calculations are separated from plotting, ideally with separate functions. You could also use the initialization block inside _get_bode_axes to customize the axes with respect to grid, labels...anything that you don't want to change as you add lines to the plot, and that you can set once with the first call to plot_bode and won't potentially need to change in subsequent calls that write to the same figure. Or you could do that sort of customization outside and after the loop that plots the lines. Eric On 2018/01/20 9:44 AM, Rory Yorke wrote: > Hi, > > I'm a contributor to the Python Control Systems Library [1], which uses > Matplotlib for plotting. > > We recently noticed deprecation warnings due to how we use > pyplot.subplot. We use it in the Matlab manner of either getting a > handle to an existing axis, or creating one if no suitable axis exists. > > The warning is > > MatplotlibDeprecationWarning: Adding an axes using the same arguments > as a previous axes currently reuses the earlier instance. In a future > version, a new instance will always be created and returned. > Meanwhile, this warning can be suppressed, and the future behavior > ensured, by passing a unique label to each axes instance. > > For example, to plot the frequency response a linear dynamical system > (AKA Bode plot of the system), the relevant function could be simplified > to: > > def bode_plot(g): > freq, mag, phase = freq_resp(g) > subplot(211) > semilogx(freq, 20*log10(mag)) > subplot(212) > semilogx(freq, phase) > > We've replaced that with code like this: > > def bode_plot(g): > freq, mag, phase = freq_resp(g) > > ax_mag = None > ax_phase = None > for ax in gcf.axes(): > if ax.get_label() == 'control-bode-magnitude': > ax_mag = ax > elif ax.get_label() == 'control-bode-phase': > ax_phase = ax > > if ax_mag is None or ax_phase is None: > clf() > ax_mag = subplot(211, label = 'control-bode-magnitude') > ax_phase = subplot(212, label = 'control-bode-phase) > > ax_mag.semilogx(freq, 20*log10(mag)) > ax_phase.semilogx(freq, phase) > > This means that calls like > > bode_plot(g) > bode_plot(h) > > will show the response of g and h on the same figure. > > Is this method of using labels to check for existing axes reasonable? > Is there a better way? > > Actual code exhibiting warnings at [2]; new code at [3]. The latter > link is to an as-yet unmerged branch, and may disappear. > > Thanks, > > Rory > > [1] https://github.com/python-control/python-control > [2] https://github.com/python-control/python-control/blob/af8d4ee39dfa574c2b3b335f4cdb4be858ae469a/control/freqplot.py#L175 > [3] https://github.com/murrayrm/python-control/blob/dc1820a4e64d73937c7de8df078c41ec1773e048/control/freqplot.py#L138 > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users > From eric at depagne.org Sat Jan 20 21:00:47 2018 From: eric at depagne.org (=?ISO-8859-1?Q?=C9ric?= Depagne) Date: Sun, 21 Jan 2018 04:00:47 +0200 Subject: [Matplotlib-users] Subplots, axes labels and mouse events. Message-ID: <2019265.07FCjpN6d0@notebook> Hi all, I have a figure which consists of three subplots, which I have defined as follows: gs = gridspec.GridSpec(2, 2) self.ax1 = plt.subplot(gs[0:, 0]) self.ax2 = plt.subplot(gs[1]) self.ax3 = plt.subplot(gs[3]) self.ax1.imshow(self.data, vmin=self.dataminzs, vmax=self.datamaxzs) self.ax1.figure.canvas.mpl_connect('motion_notify_event', self._on_move) The _on_move() callback is (currently) defined as: def _on_move(self, event): if event.inaxes: ax = event.inaxes # the axes instance print('data coords %s %f %f' % (ax, event.xdata, event.ydata)) I want that when I hover the mouse over ax1 then ax2 and ax3 show the part of ax1 which is under the cursor, with 2 different levels of zooms. I want to limit the region where the _on_move() is called to ax1 and I struggle to get what I want. I thought that calling self.ax1.figure.canvas.mpl_connect('motion_notify_event', self._on_move) would limit the area of my figure where the mouse event are caught to ax1, but it's not the case. I see that even.inaxes is picking that I'm on different part of the figure, the print('data coords %s %f %f' % (ax, event.xdata, event.ydata)) shows different values for ax depending on where the mouse is over the figure So I thought that I might be checking the attributes of event.inaxes to make sure I do what I need to do only when the mouse is over ax1, but it seems that all the attributes of the Axes are not passed to the _on_move() callback. self.ax1.set_label('AX1') sets the ax1 label as expected, but if I test the event.inaxes.properties() in my _on_move(), then the label attribute is not set anymore. Is there a way to make sure the _on_move() is fired only on a specific area of my figure? Any help appreciated. Thanks. ?ric. -- Un clavier azerty en vaut deux ---------------------------------------------------------- ?ric Depagne -------------- next part -------------- An HTML attachment was scrubbed... URL: From eric at depagne.org Sun Jan 21 14:36:35 2018 From: eric at depagne.org (=?ISO-8859-1?Q?=C9ric?= Depagne) Date: Sun, 21 Jan 2018 21:36:35 +0200 Subject: [Matplotlib-users] Subplots, axes labels and mouse events. In-Reply-To: <2019265.07FCjpN6d0@notebook> References: <2019265.07FCjpN6d0@notebook> Message-ID: <3124749.QUOuVjc6zS@notebook> Le dimanche 21 janvier 2018, 04:00:47 SAST ?ric Depagne a ?crit : Hi all, Not sure what I did wrong yesterday, but it seems that self.ax1.set_label('AX1') Indeed sets the label properly, and I can test that in the _on_move() Sorry for the noise. ?ric. > Hi all, > > I have a figure which consists of three subplots, which I have defined as > follows: gs = gridspec.GridSpec(2, 2) > self.ax1 = plt.subplot(gs[0:, 0]) > self.ax2 = plt.subplot(gs[1]) > self.ax3 = plt.subplot(gs[3]) > self.ax1.imshow(self.data, vmin=self.dataminzs, vmax=self.datamaxzs) > self.ax1.figure.canvas.mpl_connect('motion_notify_event', self._on_move) > > The _on_move() callback is (currently) defined as: > def _on_move(self, event): > if event.inaxes: > ax = event.inaxes # the axes instance > print('data coords %s %f %f' % (ax, event.xdata, event.ydata)) > > > I want that when I hover the mouse over ax1 then ax2 and ax3 show the part > of ax1 which is under the cursor, with 2 different levels of zooms. > > I want to limit the region where the _on_move() is called to ax1 and I > struggle to get what I want. > > I thought that calling > self.ax1.figure.canvas.mpl_connect('motion_notify_event', self._on_move) > would limit the area of my figure where the mouse event are caught to ax1, > but it's not the case. I see that even.inaxes is > picking that I'm on different part of the figure, the > > print('data coords %s %f %f' % (ax, event.xdata, event.ydata)) > > shows different values for ax depending on where the mouse is over the > figure > > > So I thought that I might be checking the attributes of event.inaxes to make > sure I do what I need to do only when the mouse is over ax1, but it seems > that all the attributes of the Axes are not passed to the _on_move() > callback. > > self.ax1.set_label('AX1') sets the ax1 label as expected, but if I test the > event.inaxes.properties() in my _on_move(), then the label attribute is not > set anymore. > > > Is there a way to make sure the _on_move() is fired only on a specific area > of my figure? > > Any help appreciated. > > Thanks. > > ?ric. -- Un clavier azerty en vaut deux ---------------------------------------------------------- ?ric Depagne -------------- next part -------------- An HTML attachment was scrubbed... URL: From ndbecker2 at gmail.com Tue Jan 30 10:10:32 2018 From: ndbecker2 at gmail.com (Neal Becker) Date: Tue, 30 Jan 2018 10:10:32 -0500 Subject: [Matplotlib-users] pick events don't work with twiny Message-ID: I find that pick events are fine until I add twiny. In that case, nothing happens when clicking. THe code outline is like: fig, ax = plt.subplots() ax2 = ax.twiny() ax.semilogy(...) fig.canvas.mpl_connect('pick_event', onpick) ax2_xaxis = np.linspace (*ax.get_xlim(), endpoint=True,)/conversion ax2.semilogy (ax2_xaxis, np.ones (len(ax2_xaxis)) * 1e-3, alpha=0) ax2.set_xlabel(...) ... removing all the lines about ax2 will restore pick function. From ben.v.root at gmail.com Tue Jan 30 10:55:40 2018 From: ben.v.root at gmail.com (Benjamin Root) Date: Tue, 30 Jan 2018 10:55:40 -0500 Subject: [Matplotlib-users] pick events don't work with twiny In-Reply-To: References: Message-ID: The problem is that the second axes is getting the pick events, because it is on top of the first axes. This has been a problem for a long time, and we haven't found a good solution for it. Ben Root On Tue, Jan 30, 2018 at 10:10 AM, Neal Becker wrote: > I find that pick events are fine until I add twiny. In that case, nothing > happens when clicking. > > THe code outline is like: > > fig, ax = plt.subplots() > ax2 = ax.twiny() > ax.semilogy(...) > fig.canvas.mpl_connect('pick_event', onpick) > ax2_xaxis = np.linspace (*ax.get_xlim(), endpoint=True,)/conversion > ax2.semilogy (ax2_xaxis, np.ones (len(ax2_xaxis)) * 1e-3, alpha=0) > ax2.set_xlabel(...) > ... > > removing all the lines about ax2 will restore pick function. > > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ndbecker2 at gmail.com Tue Jan 30 11:15:39 2018 From: ndbecker2 at gmail.com (Neal Becker) Date: Tue, 30 Jan 2018 11:15:39 -0500 Subject: [Matplotlib-users] pick events don't work with twiny References: Message-ID: Benjamin Root wrote: > The problem is that the second axes is getting the pick events, because it > is on top of the first axes. This has been a problem for a long time, and > we haven't found a good solution for it. > > Ben Root > That was my guess. Is there any workaround? > On Tue, Jan 30, 2018 at 10:10 AM, Neal Becker > wrote: > >> I find that pick events are fine until I add twiny. In that case, >> nothing happens when clicking. >> >> THe code outline is like: >> >> fig, ax = plt.subplots() >> ax2 = ax.twiny() >> ax.semilogy(...) >> fig.canvas.mpl_connect('pick_event', onpick) >> ax2_xaxis = np.linspace (*ax.get_xlim(), endpoint=True,)/conversion >> ax2.semilogy (ax2_xaxis, np.ones (len(ax2_xaxis)) * 1e-3, alpha=0) >> ax2.set_xlabel(...) >> ... >> >> removing all the lines about ax2 will restore pick function. >> From ben.v.root at gmail.com Tue Jan 30 11:27:35 2018 From: ben.v.root at gmail.com (Benjamin Root) Date: Tue, 30 Jan 2018 11:27:35 -0500 Subject: [Matplotlib-users] pick events don't work with twiny In-Reply-To: References: Message-ID: I seem to recall someone once messing around with the zorder of the axes object, but I forget if that actually worked. Of course the other workaround is to make the axes with the pickable objects be the twined axes. Ben Root On Tue, Jan 30, 2018 at 11:15 AM, Neal Becker wrote: > Benjamin Root wrote: > > > The problem is that the second axes is getting the pick events, because > it > > is on top of the first axes. This has been a problem for a long time, and > > we haven't found a good solution for it. > > > > Ben Root > > > > That was my guess. Is there any workaround? > > > On Tue, Jan 30, 2018 at 10:10 AM, Neal Becker > > wrote: > > > >> I find that pick events are fine until I add twiny. In that case, > >> nothing happens when clicking. > >> > >> THe code outline is like: > >> > >> fig, ax = plt.subplots() > >> ax2 = ax.twiny() > >> ax.semilogy(...) > >> fig.canvas.mpl_connect('pick_event', onpick) > >> ax2_xaxis = np.linspace (*ax.get_xlim(), endpoint=True,)/conversion > >> ax2.semilogy (ax2_xaxis, np.ones (len(ax2_xaxis)) * 1e-3, alpha=0) > >> ax2.set_xlabel(...) > >> ... > >> > >> removing all the lines about ax2 will restore pick function. > >> > > > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jklymak at uvic.ca Tue Jan 30 12:08:58 2018 From: jklymak at uvic.ca (Jody Klymak) Date: Tue, 30 Jan 2018 09:08:58 -0800 Subject: [Matplotlib-users] pick events don't work with twiny In-Reply-To: References: Message-ID: <6AB49983-9ADD-418D-A321-ED90DA02C974@uvic.ca> Eric Firing took s stab at it a couple of years ago: https://github.com/matplotlib/matplotlib/pull/2986 Obviously this is hard - if you stick a couple of artists on top of one another, deciding which one gets a mouse event is ambiguous. I forget exactly how picking architecture works, but its seems *possible* to let each axes artist accept pick events. Then they can duke it out over who actually does something with it. But I?m not sure axes can actually do that at the present. I also wonder if your `onpick` function can be made more sophisticated - I assume it knows what axis to look for picks in - maybe it could look in the twinned axis for a pick as well? Cheers, Jody > On 30 Jan 2018, at 08:27, Benjamin Root wrote: > > I seem to recall someone once messing around with the zorder of the axes object, but I forget if that actually worked. Of course the other workaround is to make the axes with the pickable objects be the twined axes. > > Ben Root > > > On Tue, Jan 30, 2018 at 11:15 AM, Neal Becker > wrote: > Benjamin Root wrote: > > > The problem is that the second axes is getting the pick events, because it > > is on top of the first axes. This has been a problem for a long time, and > > we haven't found a good solution for it. > > > > Ben Root > > > > That was my guess. Is there any workaround? > > > On Tue, Jan 30, 2018 at 10:10 AM, Neal Becker > > > wrote: > > > >> I find that pick events are fine until I add twiny. In that case, > >> nothing happens when clicking. > >> > >> THe code outline is like: > >> > >> fig, ax = plt.subplots() > >> ax2 = ax.twiny() > >> ax.semilogy(...) > >> fig.canvas.mpl_connect('pick_event', onpick) > >> ax2_xaxis = np.linspace (*ax.get_xlim(), endpoint=True,)/conversion > >> ax2.semilogy (ax2_xaxis, np.ones (len(ax2_xaxis)) * 1e-3, alpha=0) > >> ax2.set_xlabel(...) > >> ... > >> > >> removing all the lines about ax2 will restore pick function. > >> > > > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users > > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users -- Jody Klymak http://web.uvic.ca/~jklymak/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From sole at esrf.fr Wed Jan 31 02:31:50 2018 From: sole at esrf.fr (=?UTF-8?Q?V._Armando_Sol=c3=a9?=) Date: Wed, 31 Jan 2018 08:31:50 +0100 Subject: [Matplotlib-users] pick events don't work with twiny In-Reply-To: References: Message-ID: <5c4002b0-9fa8-3d09-d3b2-74ce590c0e25@esrf.fr> On 30/01/2018 17:27, Benjamin Root wrote: > I seem to recall someone once messing around with the zorder of the > axes object, but I forget if that actually worked. Of course the other > workaround is to make the axes with the pickable objects be the twined > axes. > I'm using something on those lines since some time that works for me since quite some time. Here is a code fragment: ??????????? self.ax = self.fig.add_axes([.15, .15, .75, .75], label="left") ??????????? self.ax2 = self.ax.twinx() ??????????? self.ax2.set_label("right") ??????????? # critical for picking!!!! ??????????? self.ax2.set_zorder(0) ??????????? self.ax2.set_autoscaley_on(True) ??????????? self.ax.set_zorder(1) ??????????? self.fig.sca(self.ax) Best regards, Armando From joferkington at gmail.com Wed Jan 31 08:43:29 2018 From: joferkington at gmail.com (Joe Kington) Date: Wed, 31 Jan 2018 07:43:29 -0600 Subject: [Matplotlib-users] pick events don't work with twiny In-Reply-To: <5c4002b0-9fa8-3d09-d3b2-74ce590c0e25@esrf.fr> References: <5c4002b0-9fa8-3d09-d3b2-74ce590c0e25@esrf.fr> Message-ID: If you know the artists you'd like to fire pick events for, you can work around it in a few different ways. One option is to bind to mouse events and handle firing off pick events in that callback. Here's how mpldatacursor gets around the twinx/twiny issue: https://github.com/joferkington/mpldatacursor/blob/master/mpldatacursor/datacursor.py#L658 Cheers, -Joe On Wed, Jan 31, 2018 at 1:31 AM, V. Armando Sol? wrote: > On 30/01/2018 17:27, Benjamin Root wrote: > > I seem to recall someone once messing around with the zorder of the > > axes object, but I forget if that actually worked. Of course the other > > workaround is to make the axes with the pickable objects be the twined > > axes. > > > > I'm using something on those lines since some time that works for me > since quite some time. Here is a code fragment: > > self.ax = self.fig.add_axes([.15, .15, .75, .75], > label="left") > self.ax2 = self.ax.twinx() > self.ax2.set_label("right") > # critical for picking!!!! > self.ax2.set_zorder(0) > self.ax2.set_autoscaley_on(True) > self.ax.set_zorder(1) > self.fig.sca(self.ax) > > Best regards, > > Armando > > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users at python.org > https://mail.python.org/mailman/listinfo/matplotlib-users > -------------- next part -------------- An HTML attachment was scrubbed... URL: