I want to simply draw plot. But I am having an interesting message instead of plot. It is not an error message, I have seen such a message before. The message is the following:
<matplotlib.figure.Figure at 0x1c4150890>
The code is:
import matplotlib.pyplot
x = [1,2,3,4]
y = [1,4,9,16]
fig = plt.figure()
plt.scatter(x,y)
plt.show()
Any help will be appreciated.
Seems like you are trying to print a matplotlib-Figure object as a string (print fig or something). Is the code above really what you're executing?
I had to change it to
import matplotlib.pyplot as plt
x = [1,2,3,4]
y = [1,4,9,16]
fig = plt.figure()
plt.scatter(x,y)
plt.show()
to make it work:
Besides, this is a scatter plot and not a bar chart.
It worked with the following code:
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Good luck!
Related
Using matplotlib, on Jupyter books, I want to make a figure with some plots, show it, add more plots, and show it again (old and new plots)
instead, it shows me only new plots on the new second image
This is the code I have:
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
a=np.random.rand(10,)
b=np.random.rand(10,)
fig1 = plt.figure(1)
plt.plot(a,'b')
#plt.draw();
plt.show();
plt.figure(1)
plt.plot(b,'g--')
plt.show();
left is what I have, right is what I want :
enter image description here
The question upside has been reduced to the most simplistic form, therefore I may have not explain that I do not want to having to recreate the figure each time (as it has about 15 lines to configure as I desire)
This is an example of code I DO NOT want:
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
a=np.random.rand(10,)
b=np.random.rand(10,)
c=np.random.rand(10,)
plt.plot(a, 'b')
plt.grid(True)
dig, ax = plt.subplots(1)
ax.plot(a,'b')
ax.plot(b,'g--')
dig, bx = plt.subplots(1)
bx.plot(a,'b')
bx.plot(b,'g--')
bx.plot(c,'r.')
plt.show()
this is a sort of pseudocode I would expect:
a=np.random.rand(10,)
b=np.random.rand(10,)
c=np.random.rand(10,)
my_plot = plt.figure()
my_plot.grid(True)
my_plot.addplot(a,'b')
my_plot.show()
my_plot.addplot(a,'g--')
my_plot.show()
my_plot.addplot(a,'r.')
my_plot.show()
(I know, this is not phyton/matplotlib, but I am sure something elegant like this should be possible)
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
a=np.random.rand(10,)
b=np.random.rand(10,)
c=np.random.rand(10,)
d=np.random.rand(10,)
p = [a,b,c,d]
colors = ['r','g','b', 'g--']
for i in range(len(p)):
fig, ax = plt.subplots(1)
for j in range(i + 1):
ax.plot(p[j], colors[j])
I wrote the code to plot and display a simple graph in Python:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import interactive
interactive(True)
x = np.arange(0,5,0.1)
y = np.sin(x)
plt.plot(x,y)
plt.show
And all I got is a blank screen.
And when I remove the "interactive" thing it shows no error but diplays nothing.
How can I display the graph?
(P.S: I use Python 2.7)
Remove these lines, they are not for a simple graphic:
from matplotlib import interactive
interactive(True)
And you're missing the () in the plt.show()
plt.show()
There is a syntax error. Replace plt.show with plt.show()
Just a note for others for future reference the full code should also include plt.figure() with the interactive elements removed.
Here what I came up with.
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
x = np.arange(0, 5, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()
But this may be a 3.5 problem I've not tried in 2.7
You can also plot graphs with pyformulas.
First pip install pyformulas, then
import pyformulas as pf
import numpy as np
x = np.linspace(-10,10,100)
y = x**2 + x*np.e**(np.cos(x)**2)
pf.plot(x, y)
Disclaimer: I'm the maintainer of pyformulas
I am trying to set the ticks on the axis of a matplotlib plot using latex. While the latex for the title and the labels for the x and y axis are rendering correctly, the text for the ticks do not render correctly.
The following is an example:
import matplotlib as mpl
import numpy as np
from matplotlib import pyplot as plt
mpl.rcParams["mathtext.fontset"] = "stix"
mpl.rcParams['font.family'] = 'STIXGeneral'
x = np.arange(1,4)
y = x*x
plt.plot(x, y)
plt.xlabel(r"$\mathfrak{X}$")
plt.ylabel(r"$\mathfrak{Y}$")
plt.title(r"$\mathfrak{T}$")
frame = plt.gca()
frame.set_xticklabels(r'$\mathfrak{t}_i$')
plt.show()
Running this code, I get the following result:
As you can see the x ticks are not rendered correctly.
I have also enabled text.usetex on rcParams. But that causes another problem. Here is an example:
import matplotlib as mpl
import numpy as np
from matplotlib import pyplot as plt
mpl.rcParams["mathtext.fontset"] = "stix"
mpl.rcParams['font.family'] = 'STIXGeneral'
plt.rc('text', usetex=1)
mpl.rcParams['text.latex.preamble'].append(r'\usepackage{amsfonts}')
x = np.arange(1,4)
y = x*x
plt.plot(x, y)
plt.xlabel(r"$\mathfrak{X}$")
plt.ylabel(r"$\mathfrak{Y}$")
plt.title(r"$\mathfrak{T}$")
frame = plt.gca()
frame.set_xticklabels(r"$\mathfrak{Q}$")
plt.show()
Running this code, I will get an error saying LaTeX was not able to process the following string: '$'. There will be no problem if I don't use latex for the ticks.
Should I set some other parameters to use latex for the ticks? Also, it is important for me to use "stix" font for the ticks.
I appreciate any help on this issue.
I use IPython/Matplotlib, and I want to create functions that can plot various graphs in the same plotting window. However, I have trouble with redrawing. This is my program test_plot_simple.py:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(10)
y2 = (x**2)/(10**2)
ye = (2**x)/(2**10)
fig, ax = plt.subplots()
def p_squared():
ax.plot(x,y2, 'r')
plt.show()
def p_exp():
ax.plot(x,ye, 'r')
plt.show()
I start IPython as $ python --matplotlib
On the IPython command line I do
In [1]: run test_plot_simple.py
In [2]: p_squared()
In [3]: p_exp()
After the second line, the squared graph is shown. But nothing happens after the second. Why is the plt.show() not working here?
It appears as though you call subplots without actually taking advantage of them, namely that you are trying to over plot on the same canvas. See here for a more thorough explanation. That being said, all you need is the following in order to have the functionality I think you want:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(10)
y2 = (x**2)/(10**2)
ye = (2**x)/(2**10)
def p_squared():
plt.plot(x,y2, 'r')
plt.show()
def p_exp():
plt.plot(x,ye, 'r')
plt.show()
Now both the p_squared() and p_exp() calls produce plots. Hope this helps.
After some digging I think I found the right way to go about this. It seems that show() is not really intended for this purpose, but rather draw() is. And if I want to keep it object-oriented, I should draw via my figure or my axis. It seems to me that something like this is the best approach:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(10)
y2 = (x**2)/(10**2)
ye = (2**x)/(2**10)
fig, ax = plt.subplots()
fig.show()
def p_squared():
ax.plot(x,y2, 'r')
fig.canvas.draw()
def p_exp():
ax.plot(x,ye, 'r')
fig.canvas.draw()
I.e., use fig.canvas.draw() in lieu of plt.show() (or fig.show(), for that matter.)
I still need one show() - I chose to do that right away after the figure has been created.
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.