Using .plot() functionality of pandas dataframe in a script [duplicate] - python

This question already has answers here:
How to show matplotlib plots?
(6 answers)
Closed 5 years ago.
I have a pandas data frame wit the following properties:
Name: df_name,
Concerned Column: col1
If I want to plot a column, I can execute the following code in python shell(>>>) or ipython notebook.
>>>df_name['col1'].plot(kind='bar')
However, I want to use the same function in a script and execute from command line, the plot doesn't appear.
The script I want to write looks like the following:
import pandas as pd
.
.
.
df_name=pd.read_csv('filename')
# Printing bar chart of 'col1'
df_name['col1'].plot(kind='bar')
Any Ideas how to make it execute from a script?

I think, you need to import matplotlib.pyplot and to use show method like in example.
import pandas as pd
import matplotlib.pyplot as plt
df_name=pd.DataFrame([1,2,3])
df_name[0].plot(kind='bar')
plt.show()

Related

how to create a pie-chart from csv file using python

I have this csv data file, I'm trying to create a pie chart using this data
I am a beginner in Python and don't understand how to create a pie chart using CSV
my code:
`
import pandas as pd
from matplotlib.pyplot import pie, axis, show
df = pd.read_csv ('hasil.csv')
sums = df.groupby(df["Analysis"]) ["Polarity"].sum()
axis('equal')
pie(sums, labels=sums.index)
show()
`
working solution code would be more helpful!

Can 'Seaborn' & 'Matplotlib' plots be saved in a HTML file? [duplicate]

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.

How to save a Matplotlib figure in a HTML file? [duplicate]

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

Importing from Excel in Pandas Python [duplicate]

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

plot in Pandas immediately closes

I have a problem of plotting the data. I run the following python code:
import pandas as pd
df = pd.read_csv("table.csv")
values = df["blah"]
values.plot()
print 1
df['blahblah'].plot()
print 2
My output is:
50728417:Desktop evgeniy$ python test2.py
1
2
50728417:Desktop evgeniy$
And finally I see how launches python icon( picture of rocket, by the way , what is it?) in my Dock( using mac os), and then it disappears. Printed numbers 1,2 show me that no error exist. So have no idea what to do next.
The problem is that pandas's DataFrame.plot does not block. Therefore the figures are closed when your program ends. Pandas uses matplotlib internally so you can circumvent this by calling matplotlib.pyplot.show with block=True like so:
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)
This way the program will only end when plt.show returns, which is after you closed all figures.

Categories