Camelot Matplotlib window suddenly closes - python

import camelot
import pandas as pd
import matplotlib
file = 'foo.pdf'
tables = camelot.read_pdf(file, pages='all', flavor='stream')
camelot.plot(tables[0], kind='text').show()
The matplot window opens and suddenly closes in a flash without any user input whatsoever.
I want the window to remain open to examine the contents.
Edit: I am using Windows 11 and Python 3.9, running the code on Pycharm and it's the system interpreter rather than a virtual environment.

Not sure if you ever found your answer, but I will attach what I have found from the following answer by swenzel: https://stackoverflow.com/a/33062819
The plot is opening in a non-blocking window which disappears as soon as the script finishes. You can override this by importing matplotlib and using plot.show(block=True) at the end to show the window as a blocking window, which will keep the script from continuing until closed. See his code snippet below.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("table.csv")
values = df["blah"]
values.plot()
print 1
df['blahblah'].plot()
print 2
plt.show(block=True)
Your code rewritten would look like the following:
import camelot
import pandas as pd
import matplotlib.pyplot as plt
file = 'foo.pdf'
tables = camelot.read_pdf(file, pages='all', flavor='stream')
camelot.plot(tables[0], kind='text')
plt.show(block=True)

Related

Graphs only appear in JupyterLab log?

I'm trying to manufacture a button that, when clicked, will begin a loop that triggers some data collection and several live, updating plots.
I thought I had the plots and the loops sorted out well, but as soon as I try to incorporate the button things go south. My plots are now in my JupyterLab log, not in the command line where they had been appearing (and updating) before. Has anyone encountered this issue before? Or have a potential workaround?
Here is how my code is structured:
import csv
from datetime import datetime
import pyvisa as visa
import serial
import time
from time import sleep
import numpy as np
from IPython import display
from IPython.display import clear_output
import matplotlib.pyplot as plt
import sys
import ipywidgets as widgets
import voila
from ipywidgets import ToggleButtons
from ipywidgets import Button
import asyncio
import nest_asyncio
from asyncio import coroutine
nest_asyncio.apply()
%matplotlib widget #only widget, not notebook or inline, will keep my plots interactive
async def mainexp(n, s): #these all lead to different loops that collect + plot data smoothly
plt.clf()
data = asyncio.create_task(datacollect(n, s))
graphr = asyncio.create_task(graphloopr(n, s))
graphv = asyncio.create_task(graphloopv(n, s))
await asyncio.sleep(s)
## button to start data collection + plotting
startbutton = widgets.Button(description="Start")
out = widgets.Output()
def on_button_clicked(b):
with out:
clear_output(True)
print("Hello")
asyncio.run(datarun(10, 1))
#show()
startbutton.on_click(on_button_clicked)
display(startbutton, out)
At this point my machine tells me it is collecting data, "Hello" prints...the only issue is when it comes to graphing--and it's stored as a blank axis in my list of log entries...?

my pandas and seaborn comands not responding

import pandas as pd
dataFrame = pd.read_excel("C:/Users/****/desktop/python folder/tensorflow/sheet.xlsx")
import seaborn as sbn
import matplotlib.pyplot as plt
sbn.pairplot(dataFrame)
Output is: PS C:\Users \ -----\Desktop\python folder> & C:/Users/------/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/-----/Desktop/python folder/tensorflow/tensorflow-101.py"
For your problem, try this.
import seaborn as sbn
import matplotlib.pyplot as plt
sbn.pairplot(dataFrame)
plt.show()
Graph will appear in new window, I guess. It's why your code is working in jupyter, and not in script.
ps. vsc is just a editor, so you can't use vsc for executing python. You are executing python script with your own python program.

jupyter notebook won't show images

#%%
from Utils.ConfigProvider import ConfigProvider
import os
import cv2
import numpy as np
from matplotlib import pyplot as plt
config = ConfigProvider.config()
and
#%%
inspected = cv2.imread(config.data.inspected_image_path, 0)
reference = cv2.imread(config.data.reference_image_path, 0)
diff = np.abs(inspected - reference)
plt.figure()
plt.title('inspected')
plt.imshow(inspected)
plt.show()
note config.data.inspected_image_path and config.data.reference_image_path are valid paths.
No errors appear, but no images are shown as well.
Running the same code from a python file does show the image.
I have something missing from the notebook.
This happens both when running using jupyter notebook and directly from PyCharm (pro)
How do I get to see images? all other answers I found just tell me to plt.show() but this obviously does not work.
I don't mind a cv2 solution as well.
You need to set a matplotlib backend.
You can do this with
%matplotlib inline
If you want to be able to interact with the plot, use
%matplotlib notebook

Python on Chromebook - matplotlib plot window partially displayed

I'm running python 3.5.3 64bit on my Google PixelBook with VSCode as my IDE. I'm following an online tutorial and have the following script:
import matplotlib.pyplot as plt
# import numpy as np
# import pandas as pd
x = [1,2,3]
y = [2,4,6]
plt.plot(x,y)
print(plt.get_backend())
# plt.savefig('matplotlib/image.svg')
plt.show()
I've tried doing some troubleshooting myself.
The backend is 'TkAgg'. The plot does show in a window but only part is shown
The window appears to work - I can see coordinates etc as I mouse over.
The saved plot also looks OK so assuming it's a tkinter issue rather than mathplotlib?
What have I missed?

Pop-up chart with standard python script

import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import iplot
import plotly.offline as py_offline
import pandas_datareader as web
from datetime import datetime
py_offline.init_notebook_mode()
df = web.DataReader("aapl", 'morningstar').reset_index()
trace = go.Candlestick(x=df.Date,
open=df.Open,
high=df.High,
low=df.Low,
close=df.Close)
data = [trace]
iplot(data, filename='simple_candlestick')
That code worked pretty well inside a jupyter notebook. Now, I want to execute it inside a standard python script. Once it is executed, I wanted a window to pop-up to see the graph related to that code, but it failed. How could I modify this code so that it works?
Instead of iplot() in the last line, using py_offline.plot() should open the plot in a browser window.
py_offline.plot(data, filename='simple_candlestick')
or
py_offline.plot(data)

Categories

Resources