When trying to run this little piece of code inspired from the matplotlib documentation Blend transparency with color in 2-D images:
import numpy as np
import matplotlib.pyplot as plt
test = np.random.random((300, 300))
plt.figure()
plt.imshow(test, alpha = test/test.max())
plt.show()
I end up with :
<matplotlib.image.AxesImage at 0x7f8bfe8cc610>Traceback (most recent call last):
File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_qt5.py", line 508, in _draw_idle
self.draw()
File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 388, in draw
self.figure.draw(self.renderer)
File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/figure.py", line 1709, in draw
renderer, self, artists, self.suppressComposite)
File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/image.py", line 135, in _draw_list_compositing_images
a.draw(renderer)
File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 2647, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/image.py", line 135, in _draw_list_compositing_images
a.draw(renderer)
File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/artist.py", line 38, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/image.py", line 619, in draw
renderer, renderer.get_image_magnification())
File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/image.py", line 881, in make_image
unsampled=unsampled)
File "/Users/darkvador/opt/anaconda3/lib/python3.7/site-packages/matplotlib/image.py", line 530, in _make_image
np.asarray(alpha_channel, np.float32) * out_alpha * alpha,
ValueError: operands could not be broadcast together with shapes (740,740) (300,300)
I'm under macos-catalina, with python:
Python 3.7.7 (default, Mar 26 2020, 10:32:53)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Note that the error does not occur if:
I comment the plt.show()
I use a constant alpha-value
I run this piece of code under a classic python 3.6 from the linux distribution
From the matplotlib 3.1.3 documentation, the alpha keyword argument must be a scalar value.
You should update to Matplotlib 3.3.1, because giving an array for the alpha parameters must have been implemented in between these two versions.
Your alpha array alpha/alpha.max() does not match the shape of the actual image array test. From the test code you provided, we can create an alpha array and successfully display it as an alpha-varying image with the code below. So you have to make sure the alpha array has the exact same shape as the test array.
import numpy as np
import matplotlib.pyplot as plt
test = np.random.random((300, 300))
alpha = test / test.max()
print(alpha)
plt.figure()
plt.imshow(test, alpha = alpha)
plt.show()
producing the image below.
Related
I was using pandas 0.23.4 and just upgraded to 1.0.1.
I have a code which generated a dataframe and I would plot it as a stacked bar plot df.plot(kind='bar') and as an area plot df.plot.area(). It was working fine. I decided to upgrade pandas and now neither of the plot commands work. Here is an example:
df=pd.DataFrame()
df["col1"]=[0.7,0.2,0.1,0.0]
df["col2"]=[0.1,0.5,0.2,0.2]
df['col3']=[0.1,0.0,0.1,0.8]
df.plot.area()
This gives the error TypeError: float() argument must be a string or a number, not '_NoValueType'.
I don't know how to fix this. I would appreciate any help.
Thanks!
EDIT: Full error message:
Traceback (most recent call last):
File "<ipython-input-96-b436d7233c8a>", line 1, in <module>
df.plot.area()
File "C:\Users\Anaconda3\lib\site-packages\pandas\plotting\_core.py", line 1363, in area
return self(kind="area", x=x, y=y, **kwargs)
File "C:\Users\Anaconda3\lib\site-packages\pandas\plotting\_core.py", line 847, in __call__
return plot_backend.plot(data, kind=kind, **kwargs)
File "C:\Users\Anaconda3\lib\site-packages\pandas\plotting\_matplotlib\__init__.py", line 61, in plot
plot_obj.generate()
File "C:\Users\Anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.py", line 262, in generate
self._setup_subplots()
File "C:\Users\Anaconda3\lib\site-packages\pandas\plotting\_matplotlib\core.py", line 321, in _setup_subplots
axes = fig.add_subplot(111)
File "C:\Users\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1257, in add_subplot
a = subplot_class_factory(projection_class)(self, *args, **kwargs)
File "C:\Users\Anaconda3\lib\site-packages\matplotlib\axes\_subplots.py", line 74, in __init__
self.update_params()
File "C:\Users\Anaconda3\lib\site-packages\matplotlib\axes\_subplots.py", line 136, in update_params
return_all=True)
File "C:\Users\Anaconda3\lib\site-packages\matplotlib\gridspec.py", line 467, in get_position
fig_bottom = fig_bottoms[rows].min()
File "C:\Users\Anaconda3\lib\site-packages\numpy\core\_methods.py", line 32, in _amin
return umr_minimum(a, axis, None, out, keepdims, initial)
TypeError: float() argument must be a string or a number, not '_NoValueType'
Okay, I rebooted my the computer and now everything works. No idea what was wrong before!
I am newcomer for TensorFlow 2.0, and after I load a figure, I want to plot the grayscaled figure transformed by tensorflow, unfortunately there was a error came up.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
im = tf.io.read_file('/home/1.png')
image = tf.image.decode_png(im)
image_gray = tf.image.rgb_to_grayscale(image)
plt.figure()
plt.imshow(image_gray)
Then the error pops:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/zhongl/miniconda3/envs/tf2/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2677, in imshow
None else {}), **kwargs)
File "/home/zhongl/miniconda3/envs/tf2/lib/python3.7/site-packages/matplotlib/__init__.py", line 1599, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "/home/zhongl/miniconda3/envs/tf2/lib/python3.7/site-packages/matplotlib/cbook/deprecation.py", line 369, in wrapper
return func(*args, **kwargs)
File "/home/zhongl/miniconda3/envs/tf2/lib/python3.7/site-packages/matplotlib/cbook/deprecation.py", line 369, in wrapper
return func(*args, **kwargs)
File "/home/zhongl/miniconda3/envs/tf2/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 5679, in imshow
im.set_data(X)
File "/home/zhongl/miniconda3/envs/tf2/lib/python3.7/site-packages/matplotlib/image.py", line 690, in set_data
.format(self._A.shape))
TypeError: Invalid shape (321, 327, 1) for image data
But the original transformed figure without any question.
plt.figure()
plt.imshow(image)
plt.show()
The important part of your error message is:
TypeError: Invalid shape (321, 327, 1) for image data
Obviously, TensorFlow's rgb_to_grayscale stores converted images in that way:
The size of the last dimension of the output is 1, containing the Grayscale value of the pixels.
Nevertheless, Matplotlib can't handle data in that way for grayscale images, but expects a shape like (321, 327), i.e. without single-dimensional data.
Since you're dealing with NumPy arrays here, you can use NumPy's squeeze method to get rid of the additional dimension:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
im = tf.io.read_file('/home/1.png')
image = tf.image.decode_png(im)
image_gray = tf.image.rgb_to_grayscale(image).squeeze() # <-- !
plt.figure()
plt.imshow(image_gray)
Hope that helps!
I'm a novice programmer and my hunch is that this error is due to some sort of installation or version problem, but I have no idea what. I'm running python 2.7 on OS 10.8, and just installed numpy 1.12.0 and matplotlib-1.5.1 today in an attempt to construct a heatmap.
I'm trying to run this example from the matplotlib site (http://matplotlib.org/examples/api/image_zcoord.html):
"""
Show how to modify the coordinate formatter to report the image "z"
value of the nearest pixel given x and y
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
X = 10*np.random.rand(5, 3)
fig, ax = plt.subplots()
ax.imshow(X, cmap=cm.jet, interpolation='nearest')
numrows, numcols = X.shape
def format_coord(x, y):
col = int(x + 0.5)
row = int(y + 0.5)
if col >= 0 and col < numcols and row >= 0 and row < numrows:
z = X[row, col]
return 'x=%1.4f, y=%1.4f, z=%1.4f' % (x, y, z)
else:
return 'x=%1.4f, y=%1.4f' % (x, y)
ax.format_coord = format_coord
plt.show()
A plot window appears, but nothing is displayed, and the mouseover coordinates kind of "stack" instead of refreshing and quickly become illegible. I also get this error in terminal:
AttributeError: 'numpy.ndarray' object has no attribute 'as_rgba_str'
Other, similar examples from the matplotlib site also exhibit similar behavior.
Of course, please let me know if this is a duplicate (I tried to search for an answer but didn't find anything similar to my problem, but I also might just not know what to search for).
If it is an installation error, instructions on how to fix it or a point in the right direction with detailed instructions would be much appreciated. Thank you!
Edit: Here's the traceback before the error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d- py2.7-macosx-10.8-x86_64.egg/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/figure.py", line 1262, in draw
renderer, self, dsu, self.suppressComposite)
File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/image.py", line 139, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/axes/_base.py", line 2355, in draw
mimage._draw_list_compositing_images(renderer, self, dsu)
File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/image.py", line 139, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/artist.py", line 63, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/image.py", line 472, in draw
renderer.draw_image(gc, l, b, im)
File "/usr/local/lib/python2.7/site-packages/matplotlib-1.5.1+1539.g1111c1d-py2.7-macosx-10.8-x86_64.egg/matplotlib/backends/backend_macosx.py", line 113, in draw_image
nrows, ncols, data = im.as_rgba_str()
AttributeError: 'numpy.ndarray' object has no attribute 'as_rgba_str'
Definitely some sort of install error, I installed Anaconda and the problem was fixed. For those who find this question in the future, ActivePython does not have numpy or scipy.
I cant fix your problem but I can tell you its an installation problem, the code you posted worked perfect on my install using python 2.7. though I am using windows instead of IOS
I'm trying to plot text values instead of symbols (for an MDS solution), and matplotlib.pyplot is giving me errors I don't understand. I've updated ipython and matplotlib to make sure it's not an old problem (or a problem with old versions), and I haven't been able to find any answers or reports of similar problems here (or elsewhere via google).
So, for example, after invoking ipython --pylab, if I type:
x = random.rand(4)
y = random.rand(4)
s = [str(i) for i in arange(4)+1]
text(x,y,s)
I get this error:
Traceback (most recent call last):
File "//anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in
draw_wrapper draw(artist, renderer, *args, **kwargs)
File "//anaconda/lib/python2.7/site-packages/matplotlib/figure.py", line 1034, in draw
func(*args)
File "//anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in
draw_wrapper draw(artist, renderer, *args, **kwargs)
File "//anaconda/lib/python2.7/site-packages/matplotlib/axes.py", line 2086, in draw
a.draw(renderer)
File "//anaconda/lib/python2.7/site-packages/matplotlib/artist.py", line 55, in
draw_wrapper draw(artist, renderer, *args, **kwargs)
File "//anaconda/lib/python2.7/site-packages/matplotlib/text.py", line 547, in draw
bbox, info, descent = self._get_layout(renderer)
File "//anaconda/lib/python2.7/site-packages/matplotlib/text.py", line 287, in
_get_layout key = self.get_prop_tup()
File "//anaconda/lib/python2.7/site-packages/matplotlib/text.py", line 696, in
get_prop_tup x, y = self.get_position()
File "//anaconda/lib/python2.7/site-packages/matplotlib/text.py", line 684, in
get_position x = float(self.convert_xunits(self._x))
TypeError: only length-1 arrays can be converted to Python scalars
I get the same error if I try calling text with scalars rather than vectors/lists (e.g., text(x[0],y[0],s[0]), or any number of variants of the arguments to the text function). The same thing happens:
with figtext,
if I manually import matplotlib.pyplot as plt and call plt.text, and
if I explicitly make figure and subplot objects and/or call scatter(x,y) first.
Also, for what it's worth, once this problem occurs, the error message appears again if I manually resize the figure. Possibly related is the fact that changes to figures don't update automatically, but only after I plot in another subplot or manually resize the figure. But I digress.
I've got an updated installation of Anaconda on a Mac (with Mavericks), and, as mentioned above, I'm using iPython.
plt.text expects a single x, y, and string values, not sequences. (See: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.text )
Just use a loop.
For example:
import numpy as np
import matplotlib.pyplot as plt
x, y = np.random.rand(2,4)
s = [str(i) for i in np.arange(1, 5)]
fig, ax = plt.subplots()
text = [ax.text(*item) for item in zip(x, y, s)]
plt.show()
For a subplot (self.intensity), I want to shade the area under the graph.
I tried this, hoping it was the correct syntax:
self.intensity.fill_between(arange(l,r), 0, projection)
Which I intend as to do shading for projection numpy array within (l,r) integer limits.
But it gives me an error. How do I do it correctly?
Heres the traceback:
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_wx.py", line 1289, in _onLeftButtonDown
FigureCanvasBase.button_press_event(self, x, y, 1, guiEvent=evt)
File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 1576, in button_press_event
self.callbacks.process(s, mouseevent)
File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 265, in process
proxy(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 191, in __call__
return mtd(*args, **kwargs)
File "/root/dev/spectrum/spectrum/plot_handler.py", line 55, in _onclick
self._call_click_callback(event.xdata)
File "/root/dev/spectrum/spectrum/plot_handler.py", line 66, in _call_click_callback
self.__click_callback(data)
File "/root/dev/spectrum/spectrum/plot_handler.py", line 186, in _on_plot_click
band_data = self._band_data)
File "/root/dev/spectrum/spectrum/plot_handler.py", line 95, in draw
self.intensity.fill_between(arange(l,r), 0, projection)
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 6457, in fill_between
raise ValueError("Argument dimensions are incompatible")
ValueError: Argument dimensions are incompatible
It seems like you are trying to fill the part of the projection from l to r. fill_between expects the x and y arrays to be of equal lengths, so you can not expect to fill part of the curve only.
To get what you want, you can do either of the following:
1. send only part of the projection that needs to be filled to the command; and draw the rest of the projection separately.
2. send a separate boolean array as argument that defines the sections to fill in. See the documentation!
For the former method, see the example code below:
from pylab import *
a = subplot(111)
t = arange(1, 100)/50.
projection = sin(2*pi*t)
# Draw the original curve
a.plot(t, projection)
# Define areas to fill in
l, r = 10, 50
# Fill the areas
a.fill_between(t[l:r], projection[l:r])
show()