Set output range of matplotlib - imshow - python
For a project I'm working on I have created two sets of data which is made from a function that takes two input and returns a 3rd. I am currently using a matplotlib imshow graph to show the data. As one of the data sets contains far higher values than the other data set so I was hoping to set a range for both meaning the colours would represent the same value across the two charts. Is there a good way to do this? thankyou
Here is the code I am currently using:
import matplotlib.pyplot as plt
import json
import numpy as np
with open("multi_testing\out_put\\bit_shift.txt","r") as f:
n = json.loads(f.read())
n = n[0]
inp = np.array(n)
fig, ax = plt.subplots()
im = ax.imshow(inp)
ax.invert_yaxis()
ax.set_title("bit shifting")
fig.tight_layout()
plt.show()
and here are the two data sets:
[[[7,7,7,7,7,7,7,7,7,7,7],[11,11,11,11,11,11,11,11,11,11,11],[15,15,15,15,15,15,15,15,15,15,15],[19,19,19,19,19,19,19,19,19,19,19],[23,23,23,23,23,23,23,23,23,23,23],[27,27,27,27,27,27,27,27,27,27,27],[31,31,31,31,31,31,31,31,31,31,31],[35,35,35,35,35,35,35,35,35,35,35],[39,39,39,39,39,39,39,39,39,39,39],[43,43,43,43,43,43,43,43,43,43,43],[47,47,47,47,47,47,47,47,47,47,47]]]
and
[[[10,19,26,28,33,35,35,37,40,42,42],[10,19,26,28,33,35,35,37,40,42,42],[10,19,26,28,33,35,35,37,40,42,42],[10,19,26,28,33,35,35,37,40,42,42],[10,19,26,28,33,35,35,37,40,42,42],[10,19,26,28,33,35,35,37,40,42,42],[10,19,26,28,33,35,35,37,40,42,42],[10,19,26,28,33,35,35,37,40,42,42],[10,19,26,28,33,35,35,37,40,42,42],[10,19,26,28,33,35,35,37,40,42,42],[10,19,26,28,33,35,35,37,40,42,42]]]
You can use vmin and vmax for this while using ax.imshow(). For example:
im = ax.imshow(inp, vmin=0, vmax=50)
Related
Updating plot in real time
I am giving data to a matrix (e.g. with shape 100x100) by the following code: from random import randint import matplotlib.pyplot as plt import numpy as np import random as rand tab = np.eye(100, 100) x = np.arange(0, 100, 1) plt.ion() fig = plt.figure() ax = fig.add_subplot(111) for i in range(100): for j in range(100): tab[i, j] = rand.randint(0, 254) line1, = ax.plot(x, tab[i, :], 'r-') line1.set_ydata(tab[i, j]) fig.canvas.draw() fig.canvas.flush_events() ax.lines.remove(line1) I need to update matrix using loops and upgrade plot in the same time. When loop with j ends, i-loop want to clear plot and start plotting again. Is it possible? My result: What I need:
After reading your comment i think i understood what you where trying to do the reason you got those horizontal lines was that you're setting ydata again after plotting(to a constant so its like plotting a horizontal line) consider the code below: from random import randint import matplotlib.pyplot as plt import numpy as np import random as rand tab = np.eye(100, 100) x = np.arange(0, 100, 1) plt.ion() fig = plt.figure() ax = fig.add_subplot(111) for i in range(100): for j in range(100): tab[i, j] = ((50-i/2)*(50-i/2)-(50-j)*(50-j))/100 for i in range(100): line1, = ax.plot(x, tab[i, :], 'r-') fig.canvas.draw() fig.canvas.flush_events() ax.lines.remove(line1) I used another for to instantiate the tab map (since you're using sensor data I guess that is exactly what you're doing in your code because you need to read all of the data (at least the ones for the current cross section) to be able to plot the type of graph you want. this is equivalent to reading all of the data at the beginning and then starting to plot it) (I also used simulated values instead of random values for the sake of testing) if you want to draw the data AS THEY COME FROM THE SENSOR then you must define a function to get the data of the current cross section from the sensor and return an array. Idk the library you're using for the sensor but I'm assuming the scan functions are synchronous so the function will return exactly after the input is over making the whole thing pseudo-real time from random import randint import matplotlib.pyplot as plt import numpy as np import random as rand x = np.arange(0, 100, 1) plt.ion() fig = plt.figure() ax = fig.add_subplot(111) for i in range(100): data = READ_CURRENT_CROSS_SECTION() line1, = ax.plot(x, data, 'r-') fig.canvas.draw() fig.canvas.flush_events() ax.lines.remove(line1) again, if plotting the data as the come from the sensor is your goal here it is going to depend a lot on the library you're using but except for all of that the problem with your code was that it was trying to plot while it was getting the data point by point which gives you insufficient data for plotting a cross section(hence the straight lines) (PS: there actually are some ways to pull it off like this but will be extremely slow!) So either write a function to scan the whole 2d area and return the whole map before you start plotting(which will be like my first code and the function i just said will replace lines 11-13). this takes away the real time feature but it will give you a beautiful animated plot in a short time write a function to scan each cross section and return it as a 100 element array. which makes it kind of real time but i guess is harder to implement. This is like my second code but you have to define READ_CURRENT_CROSS_SECTION yourself
Grid of plots with lines overplotted in matplotlib
I have a dataframe that consists of a bunch of x,y data that I'd like to see in scatter form along with a line. The dataframe consists of data with its form repeated over multiple categories. The end result I'd like to see is some kind of grid of the plots, but I'm not totally sure how matplotlib handles multiple subplots of overplotted data. Here's an example of the kind of data I'm working with: import pandas as pd import numpy as np import matplotlib.pyplot as plt category = np.arange(1,10) total_data = pd.DataFrame() for i in category: x = np.arange(0,100) y = 2*x + 10 data = np.random.normal(0,1,100) * y dataframe = pd.DataFrame({'x':x, 'y':y, 'data':data, 'category':i}) total_data = total_data.append(dataframe) We have x data, we have y data which is a linear model of some kind of generated dataset (the data variable). I had been able to generate individual plots based on subsetting the master dataset, but I'd like to see them all side-by-side in a 3x3 grid in this case. However, calling the plots within the loop just overplots them all onto one single image. Is there a good way to take the following code block and make a grid out of the category subsets? Am I overcomplicating it by doing the subset within the plot call? plt.scatter(total_data['x'][total_data['category']==1], total_data['data'][total_data['category']==1]) plt.plot(total_data['x'][total_data['category']==1], total_data['y'][total_data['category']==1], linewidth=4, color='black') If there's a simpler way to generate the by-category scatter plus line, I'm all for it. I don't know if seaborn has a similar or more intuitive method to use than pyplot.
You can use either sns.FacetGrid or manual plt.plot. For example: g = sns.FacetGrid(data=total_data, col='category', col_wrap=3) g = g.map(plt.scatter, 'x','data') g = g.map(plt.plot,'x','y', color='k'); Gives: Or manual plt with groupby: fig, axes = plt.subplots(3,3) for (cat, data), ax in zip(total_data.groupby('category'), axes.ravel()): ax.scatter(data['x'], data['data']) ax.plot(data['x'], data['y'], color='k') gives:
2D Color coded scatter plot with user defined color range and static colormap
I have 3 vectors - x,y,vel each having some 8k values. I also have quite a few files containing these 3 vectors. All the files have different x,y,vel. I want to get multiple scatter plots with the following conditions: Color coded according to the 3rd variable i.e vel. Once the ranges have been set for the colors (for the data from the 1st file), they should remain constant for all the remaining files. i don't want a dynamically changing (color code changing with each new file). Want to plot a colorbar. I greatly appreciate all your thoughts!! I have attached the code for a single file. import numpy as np import matplotlib.pyplot as plt # Create Map cm = plt.cm.get_cmap('RdYlBu') x,y,vel = np.loadtxt('finaldata_temp.txt', skiprows=0, unpack=True) vel = [cm(float(i)/(8000)) for i in xrange(8000)] # 8000 is the no. of values in each of x,y,vel vectors. # 2D Plot plt.scatter(x, y, s=27, c=vel, marker='o') plt.axis('equal') plt.savefig('testfig.png', dpi=300) plt.show() quit()
You will have to iterate over all your data files to get the maximum value for vel, I have added a few lines of code (that need to be adjusted to fit your case) that will do that. Therefore, your colorbar line has been changed to use the max_vel, allowing you to get rid of that code using the fixed value of 8000. Additionally, I took the liberty to remove the black edges around the points, because I find that they 'obfuscate' the color of the point. Lastly, I have added adjusted your plot code to use an axis object, which is required to have a colorbar. import numpy as np import matplotlib.pyplot as plt # This is needed to iterate over your data files import glob # Loop over all your data files to get the maximum value for 'vel'. # You will have to adjust this for your code """max_vel = 0 for i in glob.glob(<your files>,'r') as fr: # Iterate over all lines if <vel value> > max_vel: max_vel = <vel_value>""" # Create Map cm = plt.cm.get_cmap('RdYlBu') x,y,vel = np.loadtxt('finaldata_temp.txt', skiprows=0, unpack=True) # Plot the data fig=plt.figure() fig.patch.set_facecolor('white') # Here we switch to an axis object # Additionally, you can plot several of your files in the same figure using # the subplot option. ax=fig.add_subplot(111) s = ax.scatter(x,y,c=vel,edgecolor='')) # Here we assign the color bar to the axis object cb = plt.colorbar(mappable=s,ax=ax,cmap=cm) # Here we set the range of the color bar based on the maximum observed value # NOTE: This line only changes the calculated color and not the display # 'range' of the legend next to the plot, for that we need to switch to # ColorbarBase (see second code snippet). cb.setlim(0,max_vel) cb.set_label('Value of \'vel\'') plt.show() Snippet, demonstrating ColorbarBase import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl cm = plt.cm.get_cmap('RdYlBu') x = [1,5,10] y = [2,6,9] vel = [7,2,1] # Plot the data fig=plt.figure() fig.patch.set_facecolor('white') ax=fig.add_subplot(111) s = ax.scatter(x,y,c=vel,edgecolor='')) norm = mpl.colors.Normalize(vmin=0, vmax=10) ax1 = fig.add_axes([0.95, 0.1, 0.01, 0.8]) cb = mpl.colorbar.ColorbarBase(ax1,norm=norm,cmap=cm,orientation='vertical') cb.set_clim(vmin = 0, vmax = 10) cb.set_label('Value of \'vel\'') plt.show() This produces the following plot For more examples of what you can do with the colorbar, specifically the more flexible ColorbarBase, I would suggest that you check the documentation -> http://matplotlib.org/examples/api/colorbar_only.html
matplotlib plotting multiple lines in 3D
I am trying to plot multiple lines in a 3D plot using matplotlib. I have 6 datasets with x and y values. What I've tried so far was, to give each point in the data sets a z-value. So all points in data set 1 have z=1 all points of data set 2 have z=2 and so on. Then I exported them into three files. "X.txt" containing all x-values, "Y.txt" containing all y-values, same for "Z.txt". Here's the code so far: #!/usr/bin/python from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np import pylab xdata = '/X.txt' ydata = '/Y.txt' zdata = '/Z.txt' X = np.loadtxt(xdata) Y = np.loadtxt(ydata) Z = np.loadtxt(zdata) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot_wireframe(X,Y,Z) plt.show() What I get looks pretty close to what I need. But when using wireframe, the first point and the last point of each dataset are connected. How can I change the colour of the line for each data set and how can I remove the connecting lines between the datasets? Is there a better plotting style then wireframe?
Load the data sets individually, and then plot each one individually. I don't know what formats you have, but you want something like this from mpl_toolkits.mplot3d.axes3d import Axes3D import matplotlib.pyplot as plt fig, ax = plt.subplots(subplot_kw={'projection': '3d'}) datasets = [{"x":[1,2,3], "y":[1,4,9], "z":[0,0,0], "colour": "red"} for _ in range(6)] for dataset in datasets: ax.plot(dataset["x"], dataset["y"], dataset["z"], color=dataset["colour"]) plt.show() Each time you call plot (or plot_wireframe but i don't know what you need that) on an axes object, it will add the data as a new series. If you leave out the color argument matplotlib will choose them for you, but it's not too smart and after you add too many series' it will loop around and start using the same colours again. n.b. i haven't tested this - can't remember if color is the correct argument. Pretty sure it is though.
How can I make a simple 3D line with Matplotlib?
I want to generate the lines, which I get from an array in 3D. Here is the code: VecStart_x = [0,1,3,5] VecStart_y = [2,2,5,5] VecStart_z = [0,1,1,5] VecEnd_x = [1,2,-1,6] VecEnd_y = [3,1,-2,7] VecEnd_z =[1,0,4,9] import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot([VecStart_x ,VecEnd_x],[VecStart_y,VecEnd_y],[VecStart_z,VecEnd_z]) plt.show() Axes3D.plot() I get that error: ValueError: third arg must be a format string
I guess, you want to plot 4 lines. Then you can try for i in range(4): ax.plot([VecStart_x[i], VecEnd_x[i]], [VecStart_y[i],VecEnd_y[i]],zs=[VecStart_z[i],VecEnd_z[i]]) As Nicolas has suggested, do have a look at the matplotlib gallery.
The gallery is a great starting point to find out examples: http://matplotlib.org/gallery.html There is an example of 3d line plot here: http://matplotlib.org/examples/mplot3d/lines3d_demo.html You see that you need to pass to the ax.plot function 3 vectors. You are actually passing list of lists. I don't know what you mean by the Start and End sublist, but the following line should work : ax.plot(VecStart_x + VecEnd_x, VecStart_y + VecEnd_y, VecStart_z +VecEnd_z) Here I sum the sublist (concatenation) in order to have only one list by axis.