I am trying to use plt.spy(matrix) and would to like to understand what the precision parameter does here. The documentation states that any values of |Z| > precision will be plotted. But what does |Z| represent here?
|Z| means converting elements to their absolute values or taking modulus on each element.
This means plt.spy(matrix, precision = k) will plot elements from the matrix whose absolute value is greater than k.
import numpy as np
import matplotlib.pyplot as plt
data = np.array([[1, -1, 0, -10], [0, 0, 20, 0], [90, 0, 0, 1], [0, 0, 0, 0]])
fig, ax = plt.subplots(nrows=1, ncols=4)
ax[0].spy(data, precision=-0.5)
ax[1].spy(data, precision=5)
ax[2].spy(data, precision=10)
ax[3].spy(data, precision=80)
plt.show()
This gives:
However, I feel this needs to be explicitly mentioned in the documentation as it is confusing what |Z| means.
Related
I want to plot a time-series using matplotlib and plot. However, I want the line color to change depending on another discrete time-series.
income = [5000, 5005, 5010, 6000, 6060, 6120, 7000]
job = [0, 0, 0, 1, 1, 1, 2]
I tried something like:
plt.plot(income, c=job, cmap='RdBu')
but that leads to 'Line2D' object has no property 'cmap'. I also tried:
plt.scatter(range(0, len(income)), income, c=job, cmap='RdBu')
does not give the lines which is also not ideal. Is there any way to make a figure like the one below [created in Matlab] in Matplotlib?
I think colormap is useful for continuous data. For discrete it is better to use discrete color list. Thus, you can pair color to type variable:
Code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
income = np.array([5000, 5005, 5010, 6000, 6060, 6120, 7000])
x = np.arange(len(y))
job = np.array([0, 0, 0, 1, 1, 1, 2]).astype('int')
# iterate over zipped job and color
for j, c in zip(job, colors.TABLEAU_COLORS):
plt.plot(x[job == j], income[job == j], 'o-', c=c)
plt.show()
Plot:
I used TABLEAU_COLORS but you can find another color list here if you wish.
Currently i'm working on a video recommendation system which will predicts a video in a form of 0 (Negative) and 1 (positive). I successfully scrape data set from YouTube and also find sentiments of YouTube comments in the form of 0 (Negative) and 1 (positive).I encode text data of my csv using one hot encoder and get output in the form of numpy array. Now My question is how to give the numpy array as an input (X) in logistic regression ? Below are my code, output and csv(1874 X 2).
Target variable is Comments_Sentiments
#OneHotEncoding
import numpy as np
import pandas as pd
from sklearn import preprocessing
X = pd.read_csv("C:/Users/Shahnawaz Irfan/Desktop/USIrancrisis/demo.csv")
#X.head(5)
X = X.select_dtypes(include=[object])
#X.head(5)
#X.shape
#X.columns
le = preprocessing.LabelEncoder()
X_2 = X.apply(le.fit_transform)
X_2.head()
enc = preprocessing.OneHotEncoder()
enc.fit(X_2)
onehotlabels = enc.transform(X_2).toarray()
onehotlabels.shape
onehotlabels
Output is:
array([[1.],
[1.],
[1.],
...,
[1.],
[1.],
[1.]])
Can any one resolve this query by taking this numpy array as an input in logistic regression?
you can use the inverse functionenc.inverse_transform([[0, 1, 1, 0, 0], [0, 0, 0, 1, 0]]) enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]])
This question already has answers here:
How can matplotlib 2D patches be transformed to 3D with arbitrary normals?
(4 answers)
Closed 4 years ago.
I have tried few things by searching but I am missing on the understanding of vertices or something at least brain fade at the moment can some one help me I need a regular hexagon
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
fig = plt.figure(figsize=(15,9))
ax = fig.add_subplot(111, projection='3d')
x = [0, 2, 1, 1,1,1]
y = [0, 0, 1, 0, 1,1]
z = [0, 0, 0, 1,1,1]
vertices = [[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3],[0,1,2],[0,1,2]]
tupleList = list(zip(x, y, z))
poly3d = [[tupleList[vertices[ix][iy]] for iy in range(len(vertices[0]))] for ix in range(len(vertices))]
ax.scatter(x,y,z)
ax.add_collection3d(Poly3DCollection(poly3d, facecolors='w', linewidths=1, alpha=0.5))
ax.add_collection3d(Line3DCollection(poly3d, colors='k', linewidths=0.2, linestyles=':'))
plt.show()
Matplotlib is not cabable for real 3D.
The 3D stuff in matplotlib is mostly just for a nicer appearance of 2D-data.
If you need real 3D visualization i'd recommend Mayavi or VTK.
If your hexagon can not be expressed as a mathematical function of 2 variables (e.g. z = f(x,y) ) then matplotlib is the wrong tool for that.
The matplotlib.pyplot.quiver function takes a set of "origin" points and a set of "destination" points and the plots a bunch of arrows starting at the "origin" points headed in the direction of the "destination" points. However, there is a scaling factor so that the arrows don't necessarily end AT the "destination" points, they simply point in that direction.
e.g.
import matplotlib.pyplot as plt
import numpy as np
pts = np.array([[1, 2], [3, 4]])
end_pts = np.array([[2, 4], [6, 8]])
plt.quiver(pts[:,0], pts[:,1], end_pts[:,0], end_pts[:,1])
Note that the vector in the bottom left starts at (1,2) (which I want), but does not end at (2,4). This is governed by a scale parameter to the quiver function that makes the arrow longer or shorter. How do I get the arrow to end at EXACTLY (2,4)?
The quiver documentation states
To plot vectors in the x-y plane, with u and v having the same units as x and y, use angles='xy', scale_units='xy', scale=1.
Note however that u and v are understood relative to the position. Hence you would need to take the difference first.
import matplotlib.pyplot as plt
import numpy as np
pts = np.array([[1, 2], [3, 4]])
end_pts = np.array([[2, 4], [6, 8]])
diff = end_pts - pts
plt.quiver(pts[:,0], pts[:,1], diff[:,0], diff[:,1],
angles='xy', scale_units='xy', scale=1.)
plt.show()
How do I generate a line graph in Matplotlib where lines connecting the data points are only vertical and horizontal, not diagonal, giving a "blocky" look?
Note that this is sometimes called zero order extrapolation.
MWE
import matplotlib.pyplot as plt
x = [1, 3, 5, 7]
y = [2, 0, 4, 1]
plt.plot(x, y)
This gives:
and I want:
I think you are looking for plt.step. Here are some examples.