Matplotlib Syntax Issue - matplotlib

I'm trying to plot a simple graph using matplotlib, but nothing is working. I've followed the documentation but it still spits out this regardless of what i do.
plt.plot(Time, raw, colour="blue")
^SyntaxError: invalid syntax

Try the following:
import matplotlib.pyplot as plt
# set test data
time = [1, 2, 3]
raw = [3, 2, 1]
plt.plot(time, raw, color="blue")
Returns

Related

type hint npt.NDArray number of axis

Given I have the number of axes, can I specify the number of axes to the type hint npt.NDArray (from import numpy.typing as npt)
i.e. if I know it is a 3D array, how can I do npt.NDArray[3, np.float64]
On Python 3.9 and 3.10 the following does the job for me:
data = [[1, 2, 3], [4, 5, 6]]
arr: np.ndarray[Tuple[Literal[2], Literal[3]], np.dtype[np.int_]] = np.array(data)
It is a bit cumbersome, but you might follow numpy issue #16544 for future development on easier specification.
In particular, for now you must declare the full shape and can't only declare the rank of the array.
In the future something like ndarray[Shape[:, :, :], dtype] should be available.

Helvetica in Matplotlib with siunitx

I would like to create my graphs to match my LaTeX document and use the Helvetica font for both.
In LaTeX I have
\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
set.
The code in Python looks like this:
import matplotlib.pyplot as plt
import numpy as np
import locale
plt.rc('text', usetex=True)
plt.rcParams['text.latex.preamble'] = [
r'\usepackage[detect-all,locale=DE]{siunitx}', #SI-Einheiten, Komma
r'\usepackage{helvet}', #Helvetica als Schrift
r'\usepackage{icomma}']
locale.setlocale(locale.LC_NUMERIC, "de_DE.UTF-8")
plt.ticklabel_format(useLocale=True)
x = [1, 2, 3, 4]
y = [5, 6, 7.2, 8.1]
plt.plot(x, y, marker="o", label="setting1")
plt.xticks(np.arange(1.0, 4.2, step=0.5))
plt.xlabel("x (\si{\milli\metre})")
plt.ylabel("y (\si{\pascal})")
plt.legend()
plt.grid(True)
plt.savefig('test.pdf', bbox_inches='tight')
The problem is that "Pa" from the figure does not match the "Pa" in LaTeX
Adding this to my matplotlibrc file worked for me.
mathtext.fontset : custom
mathtext.it : Helvetica:italic
Also, I needed to have Helvetica-Oblique.ttf in my /usr/local/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf directory. Olga Botvinnik has some good instructions in her blog. Someday, I'll put a similar set of instructions together on mine.
Note, that you're going to have to clear out your cache under ~/.matplotlib in order to refresh this.
Matplotlib says that custom fontsets are not supported and this might all break in a future update of Matplotlib.

Applying scipy.sparse.linalg.svds returns nan values

I am starting to use the scipy.sparse library, and when I try to apply scipy.sparse.linalg.svds, I get an error if there are zero singular values.
I am doing this because in the end I am going to use very large and very sparse matrices with entries only {+1, -1} which are not square (>1100*1000 size with >0.99 sparsity), and I want to know their rank.
I know approximately what the rank is, it is almost full, so knowing only the last singular values can tell me what is the rank exactly.
This is why I chose to work with scipy.sparse.linalg.svds and set which='LM'. If the rank is not full, there will be singular values which are zero, this is my code:
import numpy as np
import scipy.sparse as sp
import scipy.sparse.linalg as la
a = np.array([[0, 0, 0], [0, 0, 0], [1, 1, -1]], dtype='d')
sp_a = sp.csc_matrix(a)
s = la.svds(sp_a, k=2, return_singular_vectors=False, which='SM')
print(s)
output is
[ nan 9.45667059e-12]
/usr/lib/python3/dist-packages/scipy/sparse/linalg/eigen/arpack/arpack.py:1849: RuntimeWarning: invalid value encountered in sqrt
s = np.sqrt(eigvals)
Any thoughts on why this happens?
Maybe there is another efficient way to know the rank, knowing that I have a large non-square very sparse matrix with almost full rank?
scipy version 1.1.0
numpy version 1.14.5
Linux platform
Thanks in advance

matplotlib scatter plot: How to use the data= argument

The matplotlib documentation for scatter() states:
In addition to the above described arguments, this function can take a data keyword argument. If such a data argument is given, the following arguments are replaced by data[]:
All arguments with the following names: ‘s’, ‘color’, ‘y’, ‘c’, ‘linewidths’, ‘facecolor’, ‘facecolors’, ‘x’, ‘edgecolors’.
However, I cannot figure out how to get this to work.
The minimal example
import matplotlib.pyplot as plt
import numpy as np
data = np.random.random(size=(3, 2))
props = {'c': ['r', 'g', 'b'],
's': [50, 100, 20],
'edgecolor': ['b', 'g', 'r']}
plt.scatter(data[:, 0], data[:, 1], data=props)
plt.show()
produces a plot with the default color and sizes, instead of the supplied one.
Anyone has used that functionality?
This seems to be an overlooked feature added about two years ago. The release notes have a short example (
https://matplotlib.org/users/prev_whats_new/whats_new_1.5.html#working-with-labeled-data-like-pandas-dataframes). Besides this question and a short blog post (https://tomaugspurger.github.io/modern-6-visualization.html) that's all I could find.
Basically, any dict-like object ("labeled data" as the docs call it) is passed in the data argument, and plot parameters are specified based on its keys. For example, you can create a structured array with fields a, b, and c
coords = np.random.randn(250, 3).view(dtype=[('a', float), ('b', float), ('c', float)])
You would normally create a plot of a vs b using
pyplot.plot(coords['a'], coords['b'], 'x')
but using the data argument it can be done with
pyplot.plot('a', 'b','x', data=coords)
The label b can be confused with a style string setting the line to blue, but the third argument clears up that ambiguity. It's not limited to x and y data either,
pyplot.scatter(x='a', y='b', c='c', data=coords)
Will set the point color based on column 'c'.
It looks like this feature was added for pandas dataframes, and handles them better than other objects. Additionally, it seems to be poorly documented and somewhat unstable (using x and y keyword arguments fails with the plot command, but works fine with scatter, the error messages are not helpful). That being said, it gives a nice shorthand when the data you want to plot has labels.
In reference to your example, I think the following does what you want:
plt.scatter(data[:, 0], data[:, 1], **props)
That bit in the docs is confusing to me, and looking at the sources, scatter in axes/_axes.py seems to do nothing with this data argument. Remaining kwargs end up as arguments to a PathCollection, maybe there is a bug there.
You could also set these parameters after scatter with the the various set methods in PathCollection, e.g.:
pc = plt.scatter(data[:, 0], data[:, 1])
pc.set_sizes([500,100,200])

matplotlib axis don't converge properly

I make a graph using matplotlib and save it as a pdf. When I zoom in there is a gap where the x- and y-axis converge. Is there any way to get rid of this?
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 2, 3])
y = np.array([1, 2, 3])
plt.scatter(x, y)
plt.savefig('Scatter_Plot.pdf')
Unfortunately I can not upload pictures here - but here is a link:
http://de.tinypic.com/r/25gckcw/8
Thanks
I've updated matplotlib 1.3.1 -> 1.4.3
Now everything looks perfect!