[Matplotlib-users] streamplot: vectors not pointing in the right direction!
Hi, I have problems with streamplot I want to use a 3d vector field in coordinates (x,y,z) stored in a numpy array, and plot slices of it with streamplot. To test it I wanted to use a vector field with arrows pointed up in the z0 region and pointed down in the z0 region. import numpy as np import matplotlib.pyplot as plt from math import * max = 100 min = -100 X = np.linspace(min, max, num=100) Y = np.linspace(min, max, num=100) Z = np.linspace(min, max, num=100) N = X.size #single components in the 3D matrix Bxa = np.zeros((N, N, N)) Bya = np.zeros((N, N, N)) Bza = np.zeros((N, N, N)) for i, x in enumerate(X): for j, y in enumerate(Y): for k, z in enumerate(Z): Bxa[ i, j, k] = 0.0 #x Bya[ i, j, k] = 0.0 #y Bza[ i, j, k] = z #I take a slice close to Y=0 Bx_sec = Bxa[:,N/2,:] By_sec = Bya[:,N/2,:] Bz_sec = Bza[:,N/2,:] fig = plt.figure() ax = fig.add_subplot(111) ax.streamplot(X, Z, Bx_sec, Bz_sec, color='b') ax.set_xlim([X.min(), X.max()]) ax.set_ylim([Z.min(), Z.max()]) plt.show() But I obtain something that looks like if I have put Bza = x! I tried to invert the order of vectors but it is unuseful! I attach the picture. Do you understand why? (the code I posted should run) Gabriele -- One dashboard for servers and applications across Physical-Virtual-Cloud Widest out-of-the-box monitoring support with 50+ applications Performance metrics, stats and reports that give you Actionable Insights Deep dive visibility with transaction tracing using APM Insight. http://ad.doubleclick.net/ddm/clk/290420510;117567292;y___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] streamplot: vectors not pointing in the right direction!
The documentation for streamplot: ``` *x*, *y* : 1d arrays an *evenly spaced* grid. *u*, *v* : 2d arrays x and y-velocities. Number of rows should match length of y, and the number of columns should match x. ``` Note that the rows in *u* and *v* should match *y*, and the columns should match *x*. I think your *u* and *v* are transposed. Cheers! Ben Root On Fri, May 22, 2015 at 2:50 AM, Gabriele Brambilla gb.gabrielebrambi...@gmail.com wrote: Hi, I have problems with streamplot I want to use a 3d vector field in coordinates (x,y,z) stored in a numpy array, and plot slices of it with streamplot. To test it I wanted to use a vector field with arrows pointed up in the z0 region and pointed down in the z0 region. import numpy as np import matplotlib.pyplot as plt from math import * max = 100 min = -100 X = np.linspace(min, max, num=100) Y = np.linspace(min, max, num=100) Z = np.linspace(min, max, num=100) N = X.size #single components in the 3D matrix Bxa = np.zeros((N, N, N)) Bya = np.zeros((N, N, N)) Bza = np.zeros((N, N, N)) for i, x in enumerate(X): for j, y in enumerate(Y): for k, z in enumerate(Z): Bxa[ i, j, k] = 0.0 #x Bya[ i, j, k] = 0.0 #y Bza[ i, j, k] = z #I take a slice close to Y=0 Bx_sec = Bxa[:,N/2,:] By_sec = Bya[:,N/2,:] Bz_sec = Bza[:,N/2,:] fig = plt.figure() ax = fig.add_subplot(111) ax.streamplot(X, Z, Bx_sec, Bz_sec, color='b') ax.set_xlim([X.min(), X.max()]) ax.set_ylim([Z.min(), Z.max()]) plt.show() But I obtain something that looks like if I have put Bza = x! I tried to invert the order of vectors but it is unuseful! I attach the picture. Do you understand why? (the code I posted should run) Gabriele -- One dashboard for servers and applications across Physical-Virtual-Cloud Widest out-of-the-box monitoring support with 50+ applications Performance metrics, stats and reports that give you Actionable Insights Deep dive visibility with transaction tracing using APM Insight. http://ad.doubleclick.net/ddm/clk/290420510;117567292;y ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users -- One dashboard for servers and applications across Physical-Virtual-Cloud Widest out-of-the-box monitoring support with 50+ applications Performance metrics, stats and reports that give you Actionable Insights Deep dive visibility with transaction tracing using APM Insight. http://ad.doubleclick.net/ddm/clk/290420510;117567292;y___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] Confused about rgb_to_hsv and hsv_to_rgb
I think you're asking how to blend a custom intensity image with an rgb image. (I'm traveling and just have my phone, so you'll have to excuse my lack of examples.) There are several ways to do this. Basically, it's analogous to blend modes in Photoshop etc. Have a look at the matplotlib.colors.LightSource.blend_overlay and blend_soft_light functions in the current github head. (And also http://matplotlib.org/devdocs/examples/specialty_plots/topographic_hillshading.html ) If you're working with 1.4.x, though, you won't have those functions. However, the math is very simple. Have a look at the code in those functions in the github head. It's basically a one liner. You'll need both the 4-band rgba image and the 1 band intensity/hillshade image to be floating point arrays scaled from 0-1. However, this is the default in matplotlib. How that helps a bit, and sorry again for the lack of examples! Joe OK, I understand. Could you suggest a way to reduce that 3D array to a 2D array and plot it with a specific colormap, while preserving the shading? I did something similar in Matlab https://mycarta.wordpress.com/2012/04/05/visualization-tips-for-geoscientists-matlab-part-ii/ But it took using some custom functions and a ton of asking and tinkering, and I'm not quite at that level with matplotlib, so any suggestion would be appreciated Thanks, Matteo On Thu, May 21, 2015 4:10 pm, Eric Firing wrote: Colormapping occurs only when you give imshow a 2-D array of numbers to be mapped; when you feed it a 3-D array of RGB values, it simply shows those colors. For colormapping to occur, it must be done on a 2-D array as a step leading up to the generation of your img_array. Eric On 2015/05/21 5:50 AM, Matteo Niccoli wrote: I posted a question on stackoverflow about creating with making my own shading effect (I want to use horizontal gradient for the shading). http://stackoverflow.com/questions/30310002/issue-creating-map-shading- in-matplotlib-imshow-by-setting-opacity-to-data-gradi Unfortunately I cannot share the data because I am using it for a manuscripts, but my notebook with full code listing and plots, here: http://nbviewer.ipython.org/urls/dl.dropbox.com/s/2pfhla9rn66lsbv/surfa ce_shading.ipynb/%3Fdl%3D0 The shading using gradient is implemented in two ways as suggested in the answer. What I do not understand is why the last plot comes out with a rainbow-like colors, when I did specify cubehelix as colormap. hsv = cl.rgb_to_hsv(img_array[:, :, :3]) hsv[:, :, 2] = tdx_n rgb = cl.hsv_to_rgb(hsv) plt.imshow(rgb[4:-3,4:-3], cmap='cubehelix') plt.show() Am I doing something wrong or is this unexpected behavior; is there a workaround? Thanks Matteo - - One dashboard for servers and applications across Physical-Virtual-Cloud Widest out-of-the-box monitoring support with 50+ applications Performance metrics, stats and reports that give you Actionable Insights Deep dive visibility with transaction tracing using APM Insight. http://ad.doubleclick.net/ddm/clk/290420510;117567292;y ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users -- One dashboard for servers and applications across Physical-Virtual-Cloud Widest out-of-the-box monitoring support with 50+ applications Performance metrics, stats and reports that give you Actionable Insights Deep dive visibility with transaction tracing using APM Insight. http://ad.doubleclick.net/ddm/clk/290420510;117567292;y ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users -- One dashboard for servers and applications across Physical-Virtual-Cloud Widest out-of-the-box monitoring support with 50+ applications Performance metrics, stats and reports that give you Actionable Insights Deep dive visibility with transaction tracing using APM Insight. http://ad.doubleclick.net/ddm/clk/290420510;117567292;y___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] streamplot: vectors not pointing in the right direction!
Thanks! you are right! I didn't notice this detail Gabriele On Fri, May 22, 2015 at 3:58 PM, Benjamin Root ben.r...@ou.edu wrote: The documentation for streamplot: ``` *x*, *y* : 1d arrays an *evenly spaced* grid. *u*, *v* : 2d arrays x and y-velocities. Number of rows should match length of y, and the number of columns should match x. ``` Note that the rows in *u* and *v* should match *y*, and the columns should match *x*. I think your *u* and *v* are transposed. Cheers! Ben Root On Fri, May 22, 2015 at 2:50 AM, Gabriele Brambilla gb.gabrielebrambi...@gmail.com wrote: Hi, I have problems with streamplot I want to use a 3d vector field in coordinates (x,y,z) stored in a numpy array, and plot slices of it with streamplot. To test it I wanted to use a vector field with arrows pointed up in the z0 region and pointed down in the z0 region. import numpy as np import matplotlib.pyplot as plt from math import * max = 100 min = -100 X = np.linspace(min, max, num=100) Y = np.linspace(min, max, num=100) Z = np.linspace(min, max, num=100) N = X.size #single components in the 3D matrix Bxa = np.zeros((N, N, N)) Bya = np.zeros((N, N, N)) Bza = np.zeros((N, N, N)) for i, x in enumerate(X): for j, y in enumerate(Y): for k, z in enumerate(Z): Bxa[ i, j, k] = 0.0 #x Bya[ i, j, k] = 0.0 #y Bza[ i, j, k] = z #I take a slice close to Y=0 Bx_sec = Bxa[:,N/2,:] By_sec = Bya[:,N/2,:] Bz_sec = Bza[:,N/2,:] fig = plt.figure() ax = fig.add_subplot(111) ax.streamplot(X, Z, Bx_sec, Bz_sec, color='b') ax.set_xlim([X.min(), X.max()]) ax.set_ylim([Z.min(), Z.max()]) plt.show() But I obtain something that looks like if I have put Bza = x! I tried to invert the order of vectors but it is unuseful! I attach the picture. Do you understand why? (the code I posted should run) Gabriele -- One dashboard for servers and applications across Physical-Virtual-Cloud Widest out-of-the-box monitoring support with 50+ applications Performance metrics, stats and reports that give you Actionable Insights Deep dive visibility with transaction tracing using APM Insight. http://ad.doubleclick.net/ddm/clk/290420510;117567292;y ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users -- One dashboard for servers and applications across Physical-Virtual-Cloud Widest out-of-the-box monitoring support with 50+ applications Performance metrics, stats and reports that give you Actionable Insights Deep dive visibility with transaction tracing using APM Insight. http://ad.doubleclick.net/ddm/clk/290420510;117567292;y___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
[Matplotlib-users] Legend cut off figure
I've tried several methods on stackoverflow (http://stackoverflow.com/questions/10101700/moving-matplotlib-legend-outside-of-the-axis-makes-it-cutoff-by-the-figure-box) and I'm still seeing issues with matplotlib cutting off my legend. The figure and code are posted below, note that I am using fig.savefig(fname,bbox_extra_artists = (lgd,),bbox_inches = tight) Also, the legend handler doesn't appear to be working correctly and the suptitle get's cut off which makes me think there's something major I'm messing up that I haven't yet found. Oddly, adding fig.tight_layout() causes overlap and the legend to get pulled back inside the figure (see second figure). Note that I'm also using mpl 1.4.3. Thanks for any help offered, and apologies for asking a question that has appeared many times! Nick http://matplotlib.1069221.n5.nabble.com/file/n45595/idp_brier_scores.jpeg http://matplotlib.1069221.n5.nabble.com/file/n45595/idp_brier_scores_tightlayout.jpeg import matplotlib.pyplot as plt import numpy as np import datetime as dt import h5py as h5 from matplotlib.legend_handler import HandlerLine2D from matplotlib.ticker import MultipleLocator,FormatStrFormatter majorLocator = MultipleLocator(5) majorFormatter = FormatStrFormatter('%d') minorLocator = MultipleLocator(1) LagLabel = ['','-3 to 3','2 to 8','7 to 13','12 to 18','17 to 23','22 to 28','27 to 33'] rc = plt.rcParams rc['font.family'] = 'arial' rc['xtick.direction'] = 'out' rc['xtick.major.width'] = 2 rc['xtick.labelsize'] = 'medium' rc['ytick.major.width'] = 2 rc['ytick.direction'] = 'out' rc['ytick.labelsize'] = 'medium' rc['grid.linewidth'] = 1 rc['grid.linestyle'] = ':' #rc['axes.labelweight'] = 'regular' rc['axes.linewidth'] = 2 rc['axes.labelsize'] = 'large' rc['legend.fancybox'] = True fig,ax = plt.subplots(3,1,sharex = True) fig.subplots_adjust(right = 0.75) l1, = ax[0].plot(BSBin1[0,:],linewidth = 2,color = '#66c2a5', marker = 'o',label = varNames[0]) l2, = ax[0].plot(BSBin1[1,:],linewidth = 2,color = '#fc8d62', marker = 'o',label = varNames[1]) l3, = ax[0].plot(BSBin1[2,:],linewidth = 2,color = '#8da0cb', marker = 'o',label = varNames[2]) l4, = ax[0].plot(BSBin1[3,:],linewidth = 2,color = '#e78ac3', marker = 'o',label = varNames[3]) l5, = ax[0].plot(BSBin1[4,:],linewidth = 2,color = '#a6d854', marker = 'o',label = varNames[4]) l1, = ax[1].plot(BSBin2[0,:],linewidth = 2,color = '#66c2a5', marker = 'o',label = varNames[0]) l2, = ax[1].plot(BSBin2[1,:],linewidth = 2,color = '#fc8d62', marker = 'o',label = varNames[1]) l3, = ax[1].plot(BSBin2[2,:],linewidth = 2,color = '#8da0cb', marker = 'o',label = varNames[2]) l4, = ax[1].plot(BSBin2[3,:],linewidth = 2,color = '#e78ac3', marker = 'o',label = varNames[3]) l5, = ax[1].plot(BSBin2[4,:],linewidth = 2,color = '#a6d854', marker = 'o',label = varNames[4]) l1, = ax[2].plot(BSBin3[0,:],linewidth = 2,color = '#66c2a5', marker = 'o',label = varNames[0]) l2, = ax[2].plot(BSBin3[1,:],linewidth = 2,color = '#fc8d62', marker = 'o',label = varNames[1]) l3, = ax[2].plot(BSBin3[2,:],linewidth = 2,color = '#8da0cb', marker = 'o',label = varNames[2]) l4, = ax[2].plot(BSBin3[3,:],linewidth = 2,color = '#e78ac3', marker = 'o',label = varNames[3]) l5, = ax[2].plot(BSBin3[4,:],linewidth = 2,color = '#a6d854', marker = 'o',label = varNames[4]) l6, = ax[0].plot(BSClimo1,linewidth = 2,color = 'k', marker = 'o',label = 'Climo') l6, = ax[1].plot(BSClimo2,linewidth = 2,color = 'k', marker = 'o',label = 'Climo') l6, = ax[2].plot(BSClimo3,linewidth = 2,color = 'k', marker = 'o',label = 'Climo') # Set Titles ax[0].set_title('a. Below Normal',fontsize = 12) ax[1].set_title('b. Normal',fontsize = 12) ax[2].set_title('c. Above Normal',fontsize = 12) ax[1].set_ylabel('Brier Score') ax[2].set_xlabel('Lag') ax[0].grid(True); ax[1].grid(True); ax[2].grid(True) ax[0].set_ylim((.1,.25)); ax[1].set_ylim((.1,.25)); ax[2].set_ylim((.1,.25)) ax[2].set_xticks(np.arange(0,31,5)) ax[2].xaxis.set_major_locator(majorLocator) ax[2].xaxis.set_minor_locator(minorLocator) ax[2].xaxis.set_ticks_position('bottom') ax[2].set_xticklabels(LagLabel,rotation = 45,ha = 'right') ax[0].xaxis.set_ticks_position('bottom') ax[1].xaxis.set_ticks_position('bottom') ax[2].xaxis.set_ticks_position('bottom') plt.suptitle('{0} Brier Score | 1979-2013'.format(season),fontsize = 14, fontweight = 'bold') handles,labels = ax[0].get_legend_handles_labels() lgd = fig.legend(handles,labels,bbox_to_anchor = (1.05,.75),loc = 'center right', handler_map = {l1: