This question already has answers here:
Dynamically serving a matplotlib image to the web using python
(6 answers)
Closed 1 year ago.
So I know that i can save my diagramm with
plt.savefig('/home/pi/test.png')
But I don't really know how to save and display my diagramm with a HTML file.
For my website it would be easier use a HTML file to display my data. So is it possible to save my diagramm in HTML and how?
If it helps here is my code:
from pandas import DataFrame
import sqlite3
import matplotlib.pyplot as plt
import pandas as pd
con = sqlite3.connect("/home/pi/test2.db")
df = pd.read_sql_query("SELECT * from data4 limit 79;",con)
df.plot(x = 'zeit', y = 'temp', kind ='line')
plt.savefig('/home/pi/test.png')
#plt.show()
I'm sorry if I did some mistakes I'm a beginner:)
As far as I know that is not possible with matplotlib But it is possible with https://mpld3.github.io/ and specifically this function: https://mpld3.github.io/modules/API.html#mpld3.fig_to_html
mpld3.fig_to_html(fig, d3_url=None, mpld3_url=None, no_extras=False, template_type='general', figid=None, use_http=False, **kwargs)
Output html representation of the figure
Related
This question already has answers here:
Output images to html using python
(3 answers)
Dynamically serving a matplotlib image to the web using python
(6 answers)
Closed 4 months ago.
I wrote a function to visualize python graphs using 'Seaborn' & 'Matplotlib' & I have been trying to store the output in HTML files. Is there a way to do so? I looked online but couldn't find any sources that talk about storing the visualizations of these libraries in HTML.
# Importing libraries
from statistics import correlation
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
def dynamic_manual_eda(df):
# Visualizing missing values
miss_viz=sns.heatmap(df.isnull(),yticklabels=False,cbar=False,cmap='viridis')
miss_plot=miss_viz.write_html("templates/missing_viz.html")
return miss_plot
eda_file = pd.read_csv('C:/Users/Churn_bank.csv')
dynamic_manual_eda(eda_file)
The error I get here is - '**AttributeError: 'AxesSubplot' object has no attribute 'write_html**''
NOTE: The visualizations should be stored only in HTML files. That's the requirement.
I know some similar questions have been asked but none have been able to answer my question or maybe my python programming skills are not that great(they are not). Ultimately I'm trying to creating a table to look like the one below, all of the "Some values" will be filled with JSON data which I do know how to import but creating the table to then export it to a PDF using FPDF is what is stumping me. I've tried pretty table and wasn't able to achieve this I tried using html but really I dont know too much html to build a table from scratch like this. so if some one could help or point in the right direction it would be appreciated.
I would recommend the using both the Pandas Library and MatplotLib
Firstly, with Pandas you can load data in from a JSON, either from a JSON file or string with the read_json(..) function documented here.
Something like:
import pandas as pd
df = pd.read_json("/path/to/my/file.json")
There is plenty of functionality withing the pandas library to manipulate your dataframe (your table) however you need.
Once you're done, you can then use MatplotLib to generate your PDF without needing any HTML
This would then become something like
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
df = pd.read_json("/path/to/my/file.json")
# Manipulate your dataframe 'df' here as required.
# Now use Matplotlib to write to a PDF
# Lets create a figure first
fig, ax = plt.subplots(figsize=(20, 10))
ax.axis('off')
the_table = ax.table(
cellText=df.values,
colLabels=df.columns,
loc='center'
)
# Now lets create the PDF and write the table to it
pdf_pages = PdfPages("/path/to/new/table.pdf")
pdf_pages.savefig(fig)
pdf_pages.close()
Hope this helps.
This question already has answers here:
Pandas read_csv from url
(6 answers)
Closed 3 years ago.
I am trying to download a web file with this link in pandas. The issue that I am having is that most tutorials show a file that can be downloaded with a particular extension on the end, which allows for you to more easily directly download it.
This link results in the download of a text file, but it cannot be easily read with conventional methods. How can I download this file directly in pandas with this link.
Data Science Acolyte, all you have to do is this!
import pandas as pd
df = pd.read_csv('ADAMS.txt')
you can try:
df = pd.read_csv('https://www6.sos.state.oh.us/ords/f?p=VOTERFTP:DOWNLOAD::FILE:NO:2:P2_PRODUCT_NUMBER:1')
Please try this code below if it works for you:
import pandas as pd
import io
import requests
url="https://www6.sos.state.oh.us/ords/f?p=VOTERFTP:DOWNLOAD::FILE:NO:2:P2_PRODUCT_NUMBER:1"
s=requests.get(url).content
c=pd.read_csv(io.StringIO(s.decode('utf-8')))
More details here.
This question already has answers here:
Reading an Excel file in python using pandas
(10 answers)
Closed 3 years ago.
I am trying to import data from an excel file. I would like to import from a specific sheet and in that specific sheet only import certain columns.
The code I am using is as follows:
%matplotlib inline
import pandas as pd
import math
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="darkgrid")
# Input Portfolio data
xl_file=pd.ExcelFile('C:/Users/e877780/Desktop/DEV/S2_SK_S_06_02_input.xlsx')
s2_0602=xl_file.parse("S.06.02")[('C0080','C0090')]
My idea is to only import columns C0080 and C0090. I I just use one, it works but also does not keep the column name.
Can someone help
Thks
You can try this:
xl_file=pd.ExcelFile('C:/Users/e877780/Desktop/DEV/S2_SK_S_06_02_input.xlsx')
sheet1=xl_file.parse("S.06.02")
s2_0602 = sheet1[['C0080','C0090']]
Hope it help
This question already has answers here:
How to save a pandas DataFrame table as a png
(13 answers)
Closed 5 years ago.
working on pandas describe function. simple enough code:
df['Revenue'].describe()
output is :
perfect. my issue is i want to be able to save this data as either a png or a table so that i can place in a single page. this is for my EDA (exploratory data analysis) i have 6 main charts or information that i want to evaluate on each feature. each chart will be a seperate png file. i will then combine into one pdf file. i iterate over 300 + features so doing one at a time is not an option especially seeing as it is done monthly.
if you know of a way to save this table as a png or other similar file format that would be great. thanks for the look
Saving as a csv or xlsx file
You may use to_csv("filename.csv") or to_excel("filename.xlsx") methods to save the file in a comma separated format and then manipulate/format it in Excel however you want.
Example:
df['Revenue'].describe().to_csv("my_description.csv")
Saving as a png file
As mentioned in the comments, this post explains how to save pandas dataframe to png file via matplot lib. In your case, this should work:
import matplotlib.pyplot as plt
from pandas.plotting import table
desc = df['Revenue'].describe()
#create a subplot without frame
plot = plt.subplot(111, frame_on=False)
#remove axis
plot.xaxis.set_visible(False)
plot.yaxis.set_visible(False)
#create the table plot and position it in the upper left corner
table(plot, desc,loc='upper right')
#save the plot as a png file
plt.savefig('desc_plot.png')