Different behaviour of axes object? - python-3.x

Maybe I am being extremely slow today, but I swear this has worked for me before and was showing a plot, now it returns a list with a single Lines object:
%matplotlib inline
import matplotlib.pyplot as plt
values = [0.1, 0.2, 0.3, 0.4, 0.5]
f = plt.figure()
ax = f.add_subplot(111)
ax.plot(values)
And I get <matplotlib.lines.Line2D at 0x7f04a33d6cc0> instead of a plot...
However, if I use this %matplotlib nbagg and do the same it works. I then stop the interaction and I get my desired result. What is going on here?
My versions:
python 3.6.5
matplotlib 2.2.2
jupyter 4.4.0
ipython 6.4.0
ipywidgets 7.2.1
Thanks

Related

trying to plot a beautiful image using the dataset

I want to plot an image something similar to this image by using the data provided here data
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import ticker
inp_data=np.loadtxt("data")
fig = plt.figure(figsize=(8.5, 12.0))
ax1 = fig.add_axes([0.25, 0.55, 0.2, 0.04])
plt.setp(ax1.spines.values(), linewidth=0.5)
ax1.minorticks_on()
ax1.imshow(inp_data,aspect='auto')
plt.show()
Here problem is that, the image is going blank even after the adjustment of vmin and vmax.
I hope experts may help me overcoming this problem and help me plotting a beautiful plot.
Thanks in advance.

plt.show() not working in spyder 3.7(Anaconda 3)

I am new to Python and Spyder. I am using Python 3.7 (Anaconda 3). I cannot get my graph after labelling x & y axis using plt.show() & also plt.draw() does not work either.
import os
import numpy as np
import matplotlib.pyplot as plt
time=[1,2,4]
data_list_Ch4=[1,3,5]
plt.plot(time, data_list_Ch4)
plt.xlabel("Time")
plt.ylabel("Channel")
plt.show()
screen shot of Spyder ide

Matplotlib.plot not found

I installed Matplotlib via Anaconda from here: https://anaconda.org/conda-forge/matplotlib
I used the very first command in Anaconda prompt.
But when I tried to plot from python (Spyder) as the following, I get the message:
ModuleNotFoundError: No module named 'matplotlib.plot'
import numpy as np
import matplotlib.plot as plt
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x,y)
I have installed numpy, pandas and such using the same method and they work well.
How can I fix this?
Thank you so much.
matplotlib.pyplot is a state-based interface to matplotlib. pyplot is mainly intended for interactive plots and simple cases of programmatic plot generation. Therefore, whenever trying to work with graphs and what is commonly known and informally often referred as matplotlib you should import matplotlib.pyplot as plt:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x,y)

interact and plotting with ipywidgets events produces many graphs

I am trying to use widget events to make an interactive graph.
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as widgets
def myplot(n):
x = np.linspace(-5, 5, 30)
y = x**n
fig, ax = plt.subplots(nrows=1, ncols=1);
ax.plot(x, y)
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show()
Interact works as expected (it changes the figure interactively):
widgets.interact(myplot, n=(0,5));
However the following snippet creates several figures that appear below as you interact with the slider.
n_widget = widgets.IntSlider(
value=2,
min=0,
max=5)
def on_value_change(change):
myplot(n=n_widget.value)
n_widget.observe(on_value_change)
display(n_widget)
Can I update the plot as if I were using widgets.interact()?
My current installation is with conda and Python 3.6 (windows machine).
ipywidgets 7.1.0
jupyter 1.0.0
jupyter_client 5.2.1
jupyter_console 5.2.0
jupyter_core 4.4.0
matplotlib 2.1.1
notebook 5.3.1
numpy 1.14.0
Note that the below is a working solution for ipywidgets version < 7.0. For a solution with ipywidgets >= 7.0 see this GitHub issue.
While in many simple cases plt.show() works nicely replacing the output of a cell, this is not always the case. When using interactive elements in Jupyter it is often more helpful to use IPython.display.display.
Here you may not want to create a new plot for each interaction. Instead just setting new data to the plot is enough. Then you may autoscale the plot for the new data and display the figure. You may use IPython.display.clear_output to clear the output once a new figure would be displayed. This ensures to have always a single plot present in the output cell, independent of the use of interact or observe.
def myplot(n):
line.set_ydata(x**n)
ax.relim()
ax.autoscale()
display(fig)
clear_output(wait=True)
Comlpete notebook:
# cell 1
%%capture
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display, clear_output
import ipywidgets as widgets
fig, ax = plt.subplots(nrows=1, ncols=1);
x = np.linspace(-5, 5, 30)
y = x**0
line, = ax.plot(x, y)
ax.set_xlabel('x')
ax.set_ylabel('y')
def myplot(n):
line.set_ydata(x**n)
ax.relim()
ax.autoscale()
display(fig)
clear_output(wait=True)
#cell2
widgets.interact(myplot, n=(0,5));
#cell3
n_widget = widgets.IntSlider(
value=2,
min=0,
max=5)
def on_value_change(change):
myplot(n=n_widget.value)
n_widget.observe(on_value_change)
display(n_widget)

Is it possible to generate proper Root Locus Plots with Python 3?

I am trying to generate root locus plots via Python 3, but the graphs that Python produces don't seem to be complete.
Here is the system to be implemented for the Root Locus;
Here is my code for the Root Locus plot;
import numpy as np
from matplotlib import pyplot as plt
import control
%matplotlib
G = control.TransferFunction((1, 1.5), (1, 11, 10, 0))
rlist, klist = control.rlocus(G)
plt.show()
And here is the graph I get;
But from the textbook I'm using, this is the plot that they have;
Is there a way to get Python to provide a plot which is closer the actual solution, or is this the best approximation possible with Python right now?
Try this,
import numpy as np
from matplotlib import pyplot as plt
import control
G = control.TransferFunction((1, 1.5), (1, 11, 10, 0))
rlist, klist = control.rlocus(G, kvect=np.linspace(100.0, -100.0, num=1000))
plt.show()
Output:
You can choose a more optimal kvect range depending on your transfer function.
kvect (list or ndarray, optional) – List of gains to use in computing diagram
Source: control.root_locus Documentation

Resources