No lines in plot using matplotlib Python - python

Here is my code:
import matplotlib.pyplot as plt
t = np.array([[0,1,2,3,4,5,6,7,8,9,10,11,12]])
g1 = np.array([[2,2.2,3,4,3.5,4.3,4.9,6,7.9,9.9,9.5,9.6,10]])
plt.figure(1)
plt.plot(t,g1)
Nothing happens. plt.show() does not help. I know it's because I use double brackets in t and g1 but I need that for my script. How do keep my double brackets, i.e. dimensions, AND being able to plot?
EDIT: OK, I had to transpose them in order to plot them - is there no way that Python automatically detects that?? (I am used to Matlab where the dimensions in this regard doesn't matter for plotting)

You can squeeze the dimensions of t and g1 when you plot them:
plt.plot(t.squeeze(), g1.squeeze())
Squeezing removes all singleton dimensions, thus the plot is with 1-dimensional arrays.
You noticed that if you transpose it, the plot works. That's because matplotlib plots columns when you feed it 2d data. Plot makes lines and there are no lines to make when all the columns only have one value. Another way to see this is to make a scatter plot.
plt.plot(t, g1, 'o')

Related

Use scientific notation for Python plots by default

Simple question: how do I get Python to use scientific notation in its plots by default? From various posts on SO I can write something like
from numpy import linspace
import matplotlib.pyplot as plt
plt.figure()
plt.plot(linspace(1e6,2e6),linspace(1e6,1e7))
plt.figure()
plt.plot(linspace(8e6,9e6),linspace(2e6,2.5e7))
plt.ticklabel_format(style='sci', axis='both', scilimits=(-2,2))
plt.show()
but ticklabel_format only acts on the last plot generated by matplotlib. (If plt.ticklabel_format() is put at the beginning of the code, I also get a blank figure showing the x,y axes.)
You can modify the default behaviour of matplotlib by edditing your "rc" file. See Customizing matplotlib.
In you case, it looks like you could adjust the item:
axes.formatter.limits : -2, 2 # use scientific notation if log10
# of the axis range is smaller than the
# first or larger than the second

Maplotlib gives the list of array outputs before showing the scatter plot in Python 3

I made a couple of plots before using Python 2.7 and everything is fine. Now I am trying to pick it up in Python 3 as I am trying to visualize some of the data output of the project I'm working on. So I tried to see if this works:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
# fake data:
a = np.random.normal(size=1000)
b = a*3 + np.random.normal(size=1000)
plt.hist2d(a, b, (50, 50), cmap=plt.cm.jet)
plt.colorbar()
The result is quite confusing for me: it shows the plot but before the plot it also shows the list of value of a and b, as shown in the picture below:
All I need is a clean graph of the plot. So what have I done wrong here? Haven't used matplotlib for a long time so I guess I have made some big mistakes here.
Thanks for your time in advance.
I am not an expert but what happens is that you are getting as results the variables that make your plot. I have run it into Spyder and on the right (variables section) I also get your results. What you need to do however, is to write explicitly "show the plot":
....
plt.colorbar
plt.show()
This will plot automatically your plot in a new window without showing all the arrays. Here some explanation Previous post.

Python matplotlib. Trying to plot binary signal, getting interpolation

Hey probably a simple question, but cant find the answer to this. I am monitoring a series of bits with a timestamp. I can plot the state of them just fine, but in the plots there are a skewed line between 0->1 and 1->0, where there should just be a straight line at the time they switch. How to avoid this skewed line? It makes it look like i have values in between when i dont.
EDIT: As pointed out below, using step instead of plot solves the problem. Thanks:)
You can use the plt.step function instead of plot,
import numpy as np
import matplotlib.pyplot as plt
def heaviside(x):
return .5*(np.sign(x)+1.)
x = np.linspace(0,100,10)
y = heaviside(np.random.random(10)-.5)
plt.step(x,y)
plt.ylim(-1.5,1.5)
plt.show()
which gives,
You can use a stemplot:
plt.stem(x, y)
or a step plot
plt.step(x, y)

pyplot.scatter(dataframe) vs. dataframe.plot(kind='scatter')

I have several pandas dataframes. I want to plot several columns against one another in separate scatter plots, and combine them as subplots in a figure. I want to label each subplot accordingly. I had a lot of trouble with getting subplot labels working, until I discovered that there are two ways of plotting directly from dataframes, as far as I know; see SO and pandasdoc:
ax0 = plt.scatter(df.column0, df.column5)
type(ax0): matplotlib.collections.PathCollection
and
ax1 = df.plot(0,5,kind='scatter')
type(ax1): matplotlib.axes._subplots.AxesSubplot
ax.set_title('title') works on ax1 but not on ax0, which returns
AttributeError: 'PathCollection' object has no attribute 'set_title'
I don't understand why the two separate ways exist. What is the purpose of the first method using PathCollections? The second one was added in 17.0; is the first one obsolete or has it a different purpose?
As you have found, the pandas function returns an axes object. The PathCollection object can be interpreted as an axes object as well using the "get current axes" function. For instance:
plot = plt.scatter(df.column0, df.column5)
ax0 = plt.gca()
type(ax0)
< matplotlib.axes._subplots.AxesSubplot at 0x10d2cde10>
A more standard way you might see this is the following:
fig = plt.figure()
ax0 = plt.add_subplot()
ax0.scatter(df.column0, df.column5)
At this point you are welcome to do "set" commands such as your set_title.
Hope this helps.
The difference between the two is that they are from different libraries. The first one is from matplotlib, the second one from pandas. They do the same, which is create a matplotlib scatter plot, but the matplotlib version returns a collection of points, whereas the pandas version returns a matplotlib subplot. This makes the matplotlib version a bit more versatile, as you can use the collection of points in another plot.

using more than one linestyle in the same trend line with matplotlib

I want to plot a line using the bold linestyle='k-' and after a certain value on the axes, I want the same line as dashed ('k--') or vice-versa. I want to show the dashed part as an extension to the bold line. One way to do this is to treat them as two individual plots and use different linestyles. I have attached the figure of an example. Just wondering if there was any other way to do this!
Yes it can be done. Following the suggestion given by #tom, one such example is:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1,2,10)
y = np.linspace(1,2,10)
plt.plot(x[:4],y[:4],'-ko',x[3:],y[3:],'--ko')
plt.show()
This produces a plot:

Categories