I'm trying to create a figure with three subplots of a combined raster image and histogram. I found a class called seaborn_image with a function (seaborn_image.imghist) to plot an image and show the corresponding raster. However, I would like to plot three of these next to each other, but this turned out harder than it seemed.
The class does have this method to create subplots, but this does not work for the imghist objects. This gives the following error:
Traceback (most recent call last):
File "/home/margot/Documents/code/plot_rasters.py", line 542, in <module>
isns.ImageGrid(imghist_col)
File "/home/margot/anaconda3/lib/python3.9/site-packages/seaborn_image/_grid.py", line 439, in __init__
self._map_img_to_grid()
File "/home/margot/anaconda3/lib/python3.9/site-packages/seaborn_image/_grid.py", line 496, in _map_img_to_grid
if _d.ndim > 2:
AttributeError: 'Figure' object has no attribute 'ndim'
<Figure size 1440x504 with 0 Axes>
I also looked into using the SeabornFig2Grid class from this issue, using the following code:
fig = plt.figure(figsize=(20,7))
fig.subplots_adjust(top=1)
fig.suptitle('Crop rasters', fontsize=18)
nrows = 1
ncols = 3
imghist_s1 = isns.imghist(test_S1[test_key], aspect=2.2, cmap=batlow, dx=10, units='m') # pixelsize =10m
imghist_ndvi = isns.imghist(test_NDVI[test_key], aspect=2.2, vmin=0.1, vmax=0.95, cmap=batlow, dx=10, units='m') # pixelsize =10m
imghist_bp = isns.imghist(test_BP[test_key]/1000, aspect=2.2, vmin=0.1, vmax=0.95, cmap=batlow, dx=10, units='m') # pixelsize =10m
gs = gridspec.GridSpec(nrows, ncols)
mg0 = SeabornFig2Grid(imghist_s1, fig, gs[0])
mg1 = SeabornFig2Grid(imghist_s1, fig, gs[1])
mg2 = SeabornFig2Grid(imghist_s1, fig, gs[2])
gs.tight_layout(fig)
plt.show()
But this gives me the same error as was posted in this issue. Unfortunately, the given solution does not work for the seaborn_image class.
If this is not possible, I could also combine the images and histograms myself and then plot them as subplots. But I haven't found any suitable way to do this yet.
Can anyone help with the errors or does anyone have a suggestion how to approach the problem?
Related
In matplotlib, the update_from method of a Line2D object can be used to copy properties from another line (see e.g. this answer). This is not working if the two lines live on different axes. The following code:
fig, (ax1, ax2) = plt.subplots(2, 1)
line1, = ax1.plot(range(10), "r.")
line2, = ax2.plot(*line1.get_xydata().T)
line2.update_from(line1)
raises
AttributeError: 'NoneType' object has no attribute 'extents'
while the traceback leaves me puzzled.
My questions are:
Why is this error raised?
How can I copy (all) Line2D properties of line1 to line2 instead?
EDIT
After a bit more testing I can say that the AttributeError above is for example raised in a Jupyter notebook session with the %matplotlib inline backend. With the %matplotlib notebook backend or in a regular Python script (e.g. with the "qt5agg" backend), the code passes without an error but line2 is "invisible" afterwards.
For completeness, the above image was created using (Anaconda) Python 3.7.9 and matplotlib 3.3.1 with:
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.use("qt5agg")
fig, (ax1, ax2) = plt.subplots(2, 1)
line1, = ax1.plot(range(10), "r.")
line2, = ax2.plot(*line1.get_xydata().T)
line2.update_from(line1)
plt.savefig("test.png")
The problem remains that I cannot copy the Line2D properties from line1 to line2.
EDIT 2
Throwing a plt.tight_layout() into the mix brings back the AttributeError.
EDIT 3
As requested in the comments, here is the traceback for the error I get with plt.tight_layout() (EDIT 2):
Traceback (most recent call last):
File "test.py", line 11, in <module>
plt.tight_layout()
File "/home/janjoswig/.pyenv/versions/miniconda3-4.7.12/envs/md379/lib/python3.7/site-packages/matplotlib/cbook/deprecation.py", line 451, in wrapper
return func(*args, **kwargs)
File "/home/janjoswig/.pyenv/versions/miniconda3-4.7.12/envs/md379/lib/python3.7/site-packages/matplotlib/pyplot.py", line 1490, in tight_layout
gcf().tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
File "/home/janjoswig/.pyenv/versions/miniconda3-4.7.12/envs/md379/lib/python3.7/site-packages/matplotlib/cbook/deprecation.py", line 411, in wrapper
return func(*inner_args, **inner_kwargs)
File "/home/janjoswig/.pyenv/versions/miniconda3-4.7.12/envs/md379/lib/python3.7/site-packages/matplotlib/figure.py", line 2615, in tight_layout
pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
File "/home/janjoswig/.pyenv/versions/miniconda3-4.7.12/envs/md379/lib/python3.7/site-packages/matplotlib/tight_layout.py", line 308, in get_tight_layout_figure
pad=pad, h_pad=h_pad, w_pad=w_pad)
File "/home/janjoswig/.pyenv/versions/miniconda3-4.7.12/envs/md379/lib/python3.7/site-packages/matplotlib/tight_layout.py", line 84, in auto_adjust_subplotpars
bb += [ax.get_tightbbox(renderer, for_layout_only=True)]
File "/home/janjoswig/.pyenv/versions/miniconda3-4.7.12/envs/md379/lib/python3.7/site-packages/matplotlib/axes/_base.py", line 4199, in get_tightbbox
if np.all(clip_extent.extents == axbbox.extents):
AttributeError: 'NoneType' object has no attribute 'extents'
It seems update_from updates too much, including the transformation and the clipbox. Maybe the error comes from the object being totally invisible after clipping to the wrong clipbox?
A workaround can be to save both before updating and setting them back:
from matplotlib import pyplot as plt
fig, (ax1, ax2) = plt.subplots(2, 1)
line1, = ax1.plot(range(10), "r.")
line2, = ax2.plot(*line1.get_xydata().T)
old_transform = line2.get_transform()
old_clipbox = line2.clipbox
line2.update_from(line1)
line2.set_transform(old_transform)
line2.clipbox = old_clipbox
plt.tight_layout()
plt.draw()
I am trying to create a scatter plot with a cbar of my data which I have stored in a .txt file. I found a piece of code here on stackoverflow and tested it to see whether it would work with my data.
The example code is as follows:
for record in range(5):
x = rand(50)
y = rand(50)
c = rand(1)[0] * np.ones(x.shape)
X.append(x)
Y.append(y)
C.append(c)
X = np.hstack(X)
Y = np.hstack(Y)
C = np.hstack(C)
ms=45
s = plt.scatter(X,Y,c=C, cmap=cm,s=ms)
cbar = plt.colorbar()
cbar.set_label('test')
plt.savefig('pics/test/test.png', dpi=300)
The above code produces the following scatter plot:
I have adapted the above simple code into something like this for my data:
cm = plt.cm.get_cmap('YlOrRd')
x, y, z = np.loadtxt('test.txt', unpack=True)
ms=45
pareto = plt.scatter(x,y,z, cmap=cm,s=ms)
cbar = plt.colorbar()
cbar.set_label('test')
plt.savefig('pics/test/test.png', dpi=300)
However, the above code returns with the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Mason\Desktop\WinPython-64bit-2.7.6.4\python-
2.7.6.amd64\lib\site-
packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in
runfile
execfile(filename, namespace)
File "F:/Optimisation/Plotting_data.py", line 28, in
<module>
d = plt.scatter(x,y,z, cmap=cm,s=ms)
TypeError: scatter() got multiple values for keyword argument 's'
>>>
Also, how could I adjust my axis limits?
You almost had it. You just need to specify that your colors pertain to the z array since scatter plots don't need a 3rd value. Just specify that c=z in your code and your good to go.
cm = plt.cm.get_cmap('YlOrRd')
x, y, z = np.loadtxt('test.txt', unpack=True)
ms=45
pareto = plt.scatter(x,y,c=z, cmap=cm,s=ms) #change to c=z
cbar = plt.colorbar()
cbar.set_label('test')
plt.savefig('pics/test/test.png', dpi=300)
And as noted by Jacob, use plt.xlim() and plt.ylim() to adjust limits.
The scatter function takes in 2 keyword arguments, not the 3 you are passing in within the line:
pareto = plt.scatter(x,y,z, cmap=cm,s=ms)
If you need to plot in 3D dimensions you could look into mplot3d.
To adjust your axis limits you could use:
plt.xlim(x_low, x_high)
plt.ylim(y_low, y_high)
Hello lovely clever people. I came across this beautiful piece of code that in theory should help me with a project I'm working on. However, each time I run it and select an option from the list, it crashes. The original piece of code was over 900 lines, and can be found here:
https://github.com/PySimpleGUI/PySimpleGUI/blob/e59b0060b6837cfb9ec3809da51e4fe04358b6ee/DemoPrograms/Demo_Matplotlib_Browser_Paned.py
I have reduced it to bare bones to see if I could get it working, but with no luck.
The goal is to get a functioning graphing options list to appear beside a canvas containing the corresponding graph and be able to update the graph based on the choice made by the user.
Thank you in advance!
I am using:
Pycharm 2.5 Community Edition
MatPlotLib version 3.1.1
PySimpleGui version 4.1
Numpy version 1.17
On Mac Os Mojave 10.14.6
I have updated everything that I could. I've also tried running it on a Windows machine, resulting in the same error message.
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasAgg
import matplotlib.backends.tkagg as tkagg
import inspect
import numpy as np
import matplotlib.pyplot as plt
import tkinter as Tk
def PyplotHistogram():
np.random.seed(0)
n_bins = 10
x = np.random.randn(1000, 3)
fig, axes = plt.subplots(nrows=2, ncols=2)
ax0, ax1, ax2, ax3 = axes.flatten()
colors = ['red', 'tan', 'lime']
ax0.hist(x, n_bins, density=1, histtype='bar', color=colors, label=colors)
ax0.legend(prop={'size': 10})
ax0.set_title('bars with legend')
ax1.hist(x, n_bins, density=1, histtype='bar', stacked=True)
ax1.set_title('stacked bar')
ax2.hist(x, n_bins, histtype='step', stacked=True, fill=False)
ax2.set_title('stack step (unfilled)')
# Make a multiple-histogram of data-sets with different length.
x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]]
ax3.hist(x_multi, n_bins, histtype='bar')
ax3.set_title('different sample sizes')
fig.tight_layout()
return fig
def draw_figure(canvas, figure, loc=(0, 0)):
""" Draw a matplotlib figure onto a Tk canvas
loc: location of top-left corner of figure on canvas in pixels.
Inspired by matplotlib source: lib/matplotlib/backends/backend_tkagg.py
"""
figure_canvas_agg = FigureCanvasAgg(figure)
figure_canvas_agg.draw()
figure_x, figure_y, figure_w, figure_h = figure.bbox.bounds
figure_w, figure_h = int(figure_w), int(figure_h)
photo = Tk.PhotoImage(master=canvas, width=figure_w, height=figure_h)
# Position: convert from top-left anchor to center anchor
canvas.create_image(loc[0] + figure_w/2, loc[1] + figure_h/2, image=photo)
# Unfortunately, there's no accessor for the pointer to the native renderer
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
# Return a handle which contains a reference to the photo object
# which must be kept live or else the picture disappears
return photo
fig_dict = {'Pyplot Histogram' : PyplotHistogram}
sg.ChangeLookAndFeel('LightGreen')
figure_w, figure_h = 650, 650
# define the form layout
listbox_values = [key for key in fig_dict.keys()]
col_listbox = [[sg.Listbox(values=listbox_values, change_submits=True, size=(28, len(listbox_values)), key='func')],
[sg.T(' ' * 12), sg.Exit(size=(5, 2))]]
col_multiline = sg.Column([[sg.Multiline(size=(70, 35), key='multiline')]])
col_canvas = sg.Column([[ sg.Canvas(size=(figure_w, figure_h), key='canvas')]])
layout = [[sg.Text('Matplotlib Plot Test', font=('current 18'))],
[sg.Column(col_listbox), sg.Pane([col_canvas, col_multiline], size=(800,600))],
]
# create the form and show it without the plot
window = sg.Window('Demo Application - Embedding Matplotlib In PySimpleGUI',resizable=True, grab_anywhere=False).Layout(layout)
window.Finalize()
canvas_elem = window.FindElement('canvas')
multiline_elem= window.FindElement('multiline')
while True:
event, values = window.Read()
# print(event)
# show it all again and get buttons
if event in (None, 'Exit'):
break
try:
choice = values['func'][0]
func = fig_dict[choice]
except:
pass
multiline_elem.Update(inspect.getsource(func))
plt.clf()
fig = func()
fig_photo = draw_figure(canvas_elem.TKCanvas, fig)
*** ERROR MESSAGE ***
/Users/thewarpdrive/PycharmProjects/sports/venv/bin/python "/Users/thewarpdrive/PycharmProjects/sports/breakdown graph options.py"
/Users/thewarpdrive/PycharmProjects/sports/breakdown graph options.py:5: MatplotlibDeprecationWarning:
The matplotlib.backends.tkagg module was deprecated in Matplotlib 3.0 and will be removed in 3.2.
import matplotlib.backends.tkagg as tkagg
*** Changing look and feel is not supported on Mac platform ***
Traceback (most recent call last):
File "/Users/thewarpdrive/PycharmProjects/sports/venv/lib/python3.7/site-packages/matplotlib/backends/tkagg.py", line 27, in blit
dataptr, colormode, bboxptr)
_tkinter.TclError: invalid command name "PyAggImagePhoto"
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/thewarpdrive/PycharmProjects/sports/breakdown graph options.py", line 103, in <module>
fig_photo = draw_figure(canvas_elem.TKCanvas, fig)
File "/Users/thewarpdrive/PycharmProjects/sports/breakdown graph options.py", line 56, in draw_figure
tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2)
File "/Users/thewarpdrive/PycharmProjects/sports/venv/lib/python3.7/site-packages/matplotlib/backends/tkagg.py", line 28, in blit
except tk.TclError:
AttributeError: '_tkinter.tkapp' object has no attribute 'TclError'
Process finished with exit code 1
There is an issue opened on the PySimpleGUI GitHub about problems using Matplotlib 3.1.1 with PySimpleGUI.
Reported and discussed in these issues.
https://github.com/PySimpleGUI/PySimpleGUI/issues/1620
https://github.com/PySimpleGUI/PySimpleGUI/issues/1713
Try installing Matplotlib 3.0.3 instead.
You may find that you can get quicker answers by searching the project's GitHub.
Here is my code:
import matplotlib.pyplot as plt
plt.figure(1) # the first figure
plt.subplot(211) # the first subplot in the first figure
plt.plot([1, 2, 3])
plt.subplot(212) # the second subplot in the first figure
plt.plot([4, 5, 6])
plt.figure(2) # a second figure
plt.plot([4, 5, 6]) # creates a subplot(111) by default
plt.text(.5,1.5,'211',figure = 211) #tring to add text in previous subplot
plt.figure(1) # figure 1 current; subplot(212) still current
plt.subplot(211) # make subplot(211) in figure1 current
plt.title('Easy as 1, 2, 3') # subplot 211 title
The error:
Traceback (most recent call last):
File "C:/Users/ezhou/Desktop/python/test3.py", line 11, in <module>
plt.text(.5,1.5,'211',figure = 211)
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 3567, in text
ret = gca().text(x, y, s, fontdict=fontdict, withdash=withdash, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\axes\_axes.py", line 619, in text
self._add_text(t)
File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 1720, in _add_text
self._set_artist_props(txt)
File "C:\Python27\lib\site-packages\matplotlib\axes\_base.py", line 861, in _set_artist_props
a.set_figure(self.figure)
File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 640, in set_figure
raise RuntimeError("Can not put single artist in "
RuntimeError: Can not put single artist in more than one figure
I was trying to understand the kwargs 'figure' in class matplotlib.text.Text(), but it will always reply 'Can not put single artist in more than one figure'. So I was confused about how to use this 'figure' kwarg. Can anyone give me some advise? Thanks!
You shouldn't pass figure as a kwarg, instead use text method of a Figure (or Axes) instance. Example:
import matplotlib.pyplot as plt
fig1, fig2 = plt.figure(1), plt.figure(2)
sp1, sp2 = fig1.add_subplot(211), fig2.add_subplot(211)
sp1.plot([1, 2, 3])
sp2.plot([0, 1, 3])
fig1.text(.5, .3, 'whole figure')
sp2.text(.5, .5, 'subplot')
Please note that coordinates are relative (0, 1).
P.S if you find matplotlib needlessly complicated (as I do), you may wish to have a look at Plotly
I am trying to plot a ROOT 2D histogram with rootpy and matplotlib.
The code I use for this is:
from rootpy.io import File
from rootpy.plotting import Hist
import rootpy.plotting.root2matplotlib as rplt
import matplotlib.pyplot as plt
inputFile = File('mydata.root', 'read')
h_response = inputFile.myfolder.response
plt.figure(figsize=(16, 10), dpi=100)
rplt.hist(h_response, label='response matrix')
h_response.Draw()
plt.xlabel('reconstructed $E_{\mathrm{T}}^{miss}$')
plt.ylabel('Generated $E_{\mathrm{T}}^{miss}$')
plt.title('Response Matrix')
plt.savefig('ResponseMatrix.png')
However, this leaves me with the error msg:
Traceback (most recent call last):
File "/storage/Dropbox/Workspace/Analysis/DailyPythonScripts/src/unfolding.py", line 66, in <module>
rplt.hist(h_response, label='response matrix')
File "/usr/local/lib/python2.7/dist-packages/rootpy-0.7.0_a0-py2.7-linux-x86_64.egg/rootpy/plotting/root2matplotlib.py", line 140, in hist
snap_zero=snap_zero)
File "/usr/local/lib/python2.7/dist-packages/rootpy-0.7.0_a0-py2.7-linux-x86_64.egg/rootpy/plotting/root2matplotlib.py", line 82, in _set_bounds
ywidth = ymax - ymin
TypeError: unsupported operand type(s) for -: 'list' and 'list'
Obviously I am using the wrong rootpy2matplotlib module, so I had a look:
The module provides: hist, bar and errorbar functions - no specific for >= 2D.
Am I missing something? Is there an easy workaround?
PS: I would like to tag this question with a 'rootpy' tag, but it is not possible. So I apologise, since this question is quite specific.
rootpy's root2matplotlib interface now provides the hist2d, imshow and contour functions for plotting 2D ROOT histograms. See the example here:
https://github.com/rootpy/rootpy/blob/master/examples/plotting/plot_matplotlib_hist2d.py
from matplotlib import pyplot as plt
from rootpy.plotting import root2matplotlib as rplt
from rootpy.plotting import Hist2D
import numpy as np
a = Hist2D(100, -3, 3, 100, 0, 6)
a.fill_array(np.random.multivariate_normal(
mean=(0, 3),
cov=np.arange(4).reshape(2, 2),
size=(1E6,)))
fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(15, 5))
ax1.set_title('hist2d')
rplt.hist2d(a, axes=ax1)
ax2.set_title('imshow')
im = rplt.imshow(a, axes=ax2)
ax3.set_title('contour')
rplt.contour(a, axes=ax3)
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(im, cax=cbar_ax)
plt.show()
I just tried your script on a ROOT file containing a TH2D histogram. Everything worked.
/opt/rootpy # cat version.txt
gives me: 0.7.0
if I check my
/usr/local/lib/python2.7/dist-packages/rootpy-dev-py2.7.egg/rootpy/plotting/root2matplotlib.py
and compare it to the error messages you get, then it looks like we are using different versions of rootpy.
Try the newest version of rootpy.