How to save Pandas pie plot to a file? - python

I have the following code:
import pandas as pd
import matplotlib
matplotlib.style.use('ggplot')
df = pd.DataFrame({ 'sample1':['foo','bar','bar','qux'], 'score':[5,9,1,7]})
sum_df = df.groupby("sample1").sum()
pie = sum_df.plot(kind="pie", figsize=(6,6), legend = False, use_index=False, subplots=True, colormap="Pastel1")
Which makes the pie chart. What I want to do then is to save it to a file.
But why this fail?
fig = pie.get_figure()
fig.savefig("~/Desktop/myplot.pdf")
I get this error:
'numpy.ndarray' object has no attribute 'get_figure'

Well pie is a numpy array because the return type for DataFrame.plot() is a numpy array of matplotlib.AxesSubplot objects.
fig = pie[0].get_figure()
fig.savefig("~/Desktop/myplot.pdf")

Claim: My solution is save the current plot which works here, but it's not a good way to do this. What #user3100115 posted is the right way to do this.
Using matplotlib.pyplot.savefig to save it:
import matplotlib.pyplot as plt
plt.savefig('pie')
You'll get a image named pie.png like this:

Related

Overlay kde plot using Seaborn displot

I'm trying to recreate a plot that I made with seaborn distplot but using displot, since distplot is being depreciated.
How do I make the displot overlay the two columns?
Here is the original code to create using distplot:
import pandas as pd
import numpy as np
import seaborn as sns
df1 = pd.DataFrame({'num1':np.random.normal(loc=0.0, scale=1.0, size=100),'num2':np.random.normal(loc=0.0, scale=1.0, size=100)})
sns.distplot(df1['num1'],hist=False,color='orange',)
sns.distplot(df1['num2'],hist=False,color='blue')
Here is the code for the plot using displot
sns.displot(data = df1, x = 'num1',color='orange', kind = 'kde')
sns.displot(data = df1, x = 'num2',color='blue', kind = 'kde')
In think your are looking for kdeplot.
sns.kdeplot(data=df1, palette=['orange', 'blue'])
Without any special layout I get this result for your example.
I set the palette argument to define the colors as you did in your example, but this is optional.

Plotting a heatmap using CSV file data in python

I have output nested dictionary variable called all_count_details_dictionary. Using that variable I saved data to the CSV file using the following command.
import pandas as pd
csv_path = '../results_v6/output_01.csv'
# creating pandas dataframe using concat mehtod to extract data from dictionary
df = pd.concat([pd.DataFrame(l) for l in all_count_details_dictionary],axis=1).T
# saving the dataframe to the csv file
df.to_csv(csv_path, index=True)
The output CSV file is just like as below
The CSV file can be download using this link
So I used the following code to plot a graph
import matplotlib.pyplot as plt
def extract_csv_gen_plot(csv_path):
length = 1503 #len(dataframe_colums_list)
data = np.genfromtxt(csv_path, delimiter=",", skip_header=True, usecols=range(3, (length+1)))
print(data)
# renaming data axes
#fig, ax = plt.subplots()
#fig.canvas.draw()
#labels =[item.get_text() for item in ax.get_xticklabels()]
#labels[1] = 'testing'
#ax.set_xticklabels(labels)
#ax.set_xticklabels(list)
#ax.set_yticklabels(list)
#plt.setp(ax.get_xticklabels(), rotation = 90)
plt.imshow(data, cmap='hot',interpolation='nearest')
plt.show()
I tried to get the column labels and case details labels into the graph axes, but it doesn't work out. Can anyone please tell me there is any other best method to plot this table into a heat map than this?
Thank you!
I would suggest using Pandas, the labels are picked up automatically:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
def extract_csv_gen_plot(csv_path):
data = pd.read_csv(csv_path, index_col=1)
data = data.drop(data.columns[[0, 1]], axis=1)
data.index.names = ['Name']
g = sns.heatmap(data)
g.set_yticklabels(g.get_yticklabels(), rotation=0)
g.set_title('Heatmap')
plt.tight_layout()
plt.show()
extract_csv_gen_plot("output_01.csv")
I recommend using Seaborn, they have a heatmap plotting function that works very well with Pandas DataFrames
import seaborn as sns
sns.heatmap(data)
https://seaborn.pydata.org/generated/seaborn.heatmap.html

Why are my subplots plotting only to the last ax?

So in Spyder IPython and in Jupyter notebook, the following code is failing to create subplots:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
mydict = {'a': [1,2,3,4], 'b':[2,3,4,5], 'c':[3,4,5,6]}
df = pd.DataFrame(mydict)
fig, axes = plt.subplots(3,1)
axes[0] = plt.plot(df.a)
axes[1] = plt.plot(df.b)
axes[2] = plt.plot(df.c)
plt.show(fig)
and it gives back the following plot:
this also happens when I copy-c copy-vd the example code from the matplotlib webpage
what I would like is the three columns in the three different subplots to be plotted
If you create your axes using plt.subplots you are using the object oriented approach in matplotlib. Then you have to call plot() on the axes object, so axes[0].plot(df.a), not plt.plot.
What you are doing is a weird hybrid between the procedural and object oriented approach and you also overwrite the axes objects that you created when you write axes[0] = plt.plot(....
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
mydict = {'a': [1,2,3,4], 'b':[2,3,4,5], 'c':[3,4,5,6]}
df = pd.DataFrame(mydict)
fig, axes = plt.subplots(3,1)
axes[0].plot(df.a)
axes[1].plot(df.b)
axes[2].plot(df.c)
plt.show()

how to create a pie chart from csv file using python

I have this CSV data file, I'm trying to make a pie chart using this data
I'm a beginner in python and don't understand how to create a pie chart using the three columns, please help!
working solution code would be more helpful!
My code:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv ('chart_work.csv')
product_data = df["Product Name;"]
bug_data = df["Number Of Bugs"]
colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#8c564b"]
plt.pie(bug_data , labels=product_data , colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)
plt.show()
the pie chart which is outputed by this code is distorted, any help?
Chart I'm getting:
This is very simple.
import pandas as pd
from matplotlib.pyplot import pie, axis, show
%matplotlib inline
df = pd.read_csv ('chart_work.csv')
sums = df.groupby(df["Product Name;"])["Number Of Bugs"].sum()
axis('equal');
pie(sums, labels=sums.index);
show()
The pie chart does not 'know' that you want all items with same product name grouped and summed over in your chart. so you have to do that first:
df = df.groupby(["Product Name;"]).sum()
This sets the product name column as index of the df so change your product_data column selection to this:
product_data = df.index
import matplotlib.pyplot as plt
sizes=[89,80,90,100,75]
lables=["swetha","yokesh","iswarya","ranjeeth","deepika"]
plt.pie(sizes,lables=lables,autopct="%.2f")
plt.axes().set_aspect("equal")
plt.show()

How to plot a Python Dataframe with category values like this picture?

How can I achieve that using matplotlib?
Here is my code with the data you provided. As there's no class [they are all different, despite your first example in your question does have classes], I gave colors based on the numbers. You can definitely start alone from here, whatever result you want to achieve. You just need pandas, seaborn and matplotlib:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# import xls
df=pd.read_excel('data.xlsx')
# exclude Ranking values
df1 = df.ix[:,1:-1]
# for each element it takes the value of the xls cell
df2=df1.applymap(lambda x: float(x.split('\n')[1]))
# now plot it
df_heatmap = df2
fig, ax = plt.subplots(figsize=(15,15))
sns.heatmap(df_heatmap, square=True, ax=ax, annot=True, fmt="1.3f")
plt.yticks(rotation=0,fontsize=16);
plt.xticks(fontsize=12);
plt.tight_layout()
plt.savefig('dfcolorgraph.png')
Which produces the following picture.

Categories

Resources