pyqtgraph: plot with a similar functionality as matplotlib drawstyle - python

Is there any possibility within pyqtgraph to get the blue graph instead of the red one that is standard method in matplotlib AND pyqtgraph in the following matplotlib example:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,1,0.1)
y = [0,0,0,2,2,8,9,2,0,0]
plt.plot(x,y,color='red',drawstyle='standard')
plt.plot(x,y,color='blue',drawstyle='steps-post')
plt.show()
So far I did not find a way to create the blue graph in pyqtgraph with the given data x and y.
Thanks a lot
Regards
Michael

This is described in the pyqtgraph 'histogram' example. This is the translation:
import pyqtgraph as pg
import numpy as np
x = np.arange(0,1,0.1)
y = [0,0,0,2,2,8,9,2,0,0]
print len(x), len(y)
plt = pg.plot(x,y,pen='r')
curve2 = pg.PlotCurveItem(x,y[:-1],pen='b',stepMode=True)
plt.addItem(curve2)

Related

Create a detailed svg graph with matplotlib

I use the Python library matplotlib to draw a graph with a lot of data. Upon executing plt.show() I can zoom in and see the details of the graph. However, I would like to save the graph into a svg file with plt.savefig and see these details which by default are not visible from the default non-zoomed-in view. How can I do that?
Please, note that increasing DPI or inch by inch dimensions is meaningless when working with vector graphics formats such as the svg file.
As an example, consider the following program.
import matplotlib.pyplot as plt
import numpy as np
import math
x = np.arange(0,100,0.00001)
y = x*np.sin(2*math.pi*(x**1.2))
plt.plot(y)
plt.savefig('test.svg')
We will get the following plot which even when we zoom, we cannot see the details of the sine wave periods.
But we can see the details of the sine wave when displaying the image with plt.show instead and then zooming in.
Add the size of the figure:
import matplotlib.pyplot as plt
import numpy as np
import math
x = np.arange(0,100,0.00001)
y = x*np.sin(2*math.pi*(x**1.2))
fig = plt.figure(figsize=(19.20,10.80))
plt.plot(y)
plt.savefig('test.svg')
and you get the kind of resolution you wish.
As correctly observed by JohanC, another good solution is to reduce the width of the line:
import matplotlib.pyplot as plt
import numpy as np
import math
x = np.arange(0,100,0.00001)
y = x*np.sin(2*math.pi*(x**1.2))
#fig = plt.figure(figsize=(19.20,10.80))
plt.plot(y, linewidth=0.1)
plt.savefig('test.svg')

Finding certain plotting style

I am looking for a plotting function in matplotlib that plots the y-values as bars just like in an autocorrelogram but for a general function. Is there a method to do this in matplotlib or do I have to write my own function?
You could use stem
import numpy as np; np.random.seed(21)
import matplotlib.pyplot as plt
x = np.linspace(5,75)
y = np.random.randn(len(x))
plt.stem(x,y, linefmt="k", markerfmt="none", basefmt="C0", use_line_collection=True)
plt.show()

matplotlib aspect ratio for narrow matrices

I have a 200x3 matrix in python which I would like to plot. However, by using Matplotlib I get the following figure. How can I plot an image which looks nicer?
my code:
import matplotlib.pyplot as plt
plt.imshow(spectrum_matrix)
plt.show()
You can use set_aspect():
import matplotlib.pyplot as plt
import numpy as np
spectrum_matrix = np.random.rand(200,3)
plt.imshow(spectrum_matrix)
plt.axes().set_aspect('auto')
plt.show()
Output:

Function for ploting a matrix in ipython using matplotlib

I have created a matrix:
s1=np.random.randn(1000,1000)
v1=la.eigvals(s1)
matrix1=np.matrix(v1)
I want to plot the matrix in ipython.
What appropriate matplotlib function should I use?
A very simple example to "plot" a matrix:
import numpy as np
import pylab as plt
S = np.random.randn(100,100)
# Make symmetric so everything is real
S += S.T
W,V = np.linalg.eigh(S)
import pylab as plt
plt.imshow(V,interpolation="none")
plt.show()

Filling region between curve and x-axis in Python using Matplotlib

I am trying to simply fill the area under the curve of a plot in Python using MatPlotLib.
Here is my SSCCE:
import json
import pprint
import numpy as np
import matplotlib.pyplot as plt
y = [0,0,0,0,0,0,0,0,0,0,0,863,969,978,957,764,767,1009,1895,980,791]
x = np.arange(len(y))
fig2, ax2 = plt.subplots()
ax2.fill(x, y)
plt.savefig('picForWeb.png')
plt.show()
The attached picture shows the output produced.
Does anyone know why Python is not filling the entire area in between the x-axis and the curve?
I've done Google and StackOverflow searches, but could not find a similar example. Intuitively it seems that it should fill the entire area under the curve.
I usually use the fill_between function for these kinds of plots. Try something like this instead:
import numpy as np
import matplotlib.pyplot as plt
y = [0,0,0,0,0,0,0,0,0,0,0,863,969,978,957,764,767,1009,1895,980,791]
x = np.arange(len(y))
fig, (ax1) = plt.subplots(1,1);
ax1.fill_between(x, 0, y)
plt.show()
See more examples here.
If you want to use this on a pd.DataFrame use this:
df.abs().interpolate().plot.area(grid=1, linewidth=0.5)
interpolate() is optional.
plt.fill assumes that you have a closed shape to fill - interestingly if you add a final 0 to your data you get a much more sensible looking plot.
import numpy as np
import matplotlib.pyplot as plt
y = [0,0,0,0,0,0,0,0,0,0,0,863,969,978,957,764,767,1009,1895,980,791,0]
x = np.arange(len(y))
fig2, ax2 = plt.subplots()
ax2.fill(x, y)
plt.savefig('picForWeb.png')
plt.show()
Results in:
Hope this helps to explain your odd plot.

Categories