Seaborn plots clashes - python

I tried to generate 2 plots using seaborn and the 2nd plot i got seemed to be overlaying the first plot
Here's the code
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset('iris')
sns.pairplot(iris)
plt.savefig("Pairplot")
sns.heatmap(iris.corr())
plt.savefig("heatmap")
Output
1st plot
2nd plot

You could create a new figure prior to plotting the heatmap:
plt.figure()
sns.heatmap(iris.corr())

Related

Plot pandas all columns from and use their dataframe

I would like to have every column on my x-Axis and every value on my y-Axis.
With plotly and seaborn I could only find a way to plot the values against each other (column 1 on x vs coulmn 2 on y).
So for my shown example following would be columns:
"Import Files", "Defining Variables", "Simulate Cutting Down",...
I would like to have all theri values on the y-Axis.
So what I basically want is
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('timings.csv')
df.T.plot()
plt.show()
but with scatter. Matplotlib, Seaborn or Plotly is fine by me.
This would be an example for a csv File, since I can't upload a file:
Import Files,Defining Variables,Copy All Cutters,Simulate Cutting Down,Calculalte Circle, Simulate Cutting Circle, Calculate Unbalance,Write to CSV,Total Time
0.015956878662109375,0.0009989738464355469,0.022938966751098633,0.1466083526611328,0.0009968280792236328,48.128061294555664,0.0,0.014995098114013672,48.33055639266968
0.015958786010742188,0.0,0.024958133697509766,0.14598894119262695,0.0,49.22848296165466,0.0,0.004987239837646484,49.42037606239319
0.015943288803100586,0.0,0.036900997161865234,0.14561033248901367,0.0,46.80884146690369,0.0,0.004009723663330078,47.011305809020996
I only used the data you provided; as mentioned by others in the comments, barplot is more suited for this data but here it is with scatter plot:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(16,5))
sns.scatterplot(data=df.melt(), x='variable', y ='value', ax=ax)
ax.set_xlabel('')
ax.set_ylabel('Time in seconds')

create a scatter plot from a csv file with categories

I only just stepped into Python and I'm trying to generate a scatter plot with several categories. I am using the Iris Dataset as a basis, here is what I have so far:
import matplotlib.pyplot as plt
import numpy as np
fig,ax = plt.subplots(1)
iris_data = np.genfromtxt(
"iris.csv", names=True,
dtype="float", delimiter=",")
x=iris_data["sepal_length"]
y=iris_data["sepal_width"]
g=iris_data["class"]
plt.scatter(x,y)
plt.show()
I don't know how to separate out the classes and plot each one on the same graph.
I am coming from Matlab where all I needed was the
"gscatter(x,y,g) creates a scatter plot of x and y, grouped by g" to get the job done, but I am finding out that python needs a little more to get the group by g part done.
Thank you in advance for any help.
Use seaborn:
import seaborn as sns, matplotlib.pyplot as plt
iris = sns.load_dataset('iris')
sns.scatterplot(x='sepal_length',y='sepal_width',data=iris,hue='species')
plt.show()
Result:
You can do the same in matplotlib as in the example below as long as your data is organized in a pandas DataFrame:
for k,g in iris.groupby('species'):
plt.scatter(g['sepal_length'],g['sepal_width'],label=k)

Pandas plot gives mismatched x-axis with secondary_y=True

I am trying to plot two Pandas Series with matching indexes on the same plot (one as a line and the other as bars) using the df.plot() syntax. But when I add the bar plot the line plot axis ends up wrong. Why isn't this working?
import pandas as pd
import matplotlib.pyplot as plt
plt.figure()
df = pd.Series({0.142:5,0.3643:1,0.5523:2})
df.plot(use_index=True)
df.plot(kind='bar',secondary_y=True,color='b',use_index=True)

How to color individual points on scatter plots based on their type using matplotlib

I'm working on the Iris data and trying to use scatter plot, while I was able to get the output, I'd like to know how I can color the points based on their species, using matplotlib.
I've using the following syntax:
iris.plot.scatter(x='petal_length', y='petal_width')
iris.plot(kind='scatter', x='sepal_length', y='sepal_width')
Also is there any way to use a single line of code to create two scatter plots for sepal_length/width and petal_length/width while coloring based on species?
Getting the colors correct in a single call to the plotting function is a bit tedious.
import seaborn as sns
iris = sns.load_dataset("iris")
import numpy as np
import matplotlib.pyplot as plt
u, inv = np.unique(iris.species.values, return_inverse=True)
ax = iris.plot.scatter(x='petal_length', y='petal_width',
c=inv, cmap="brg", colorbar=False)
plt.show()
I would hence recommend to loop over the species, with the additional advantage of being able to easily put a legend into the plot.
import seaborn as sns
iris = sns.load_dataset("iris")
import matplotlib.pyplot as plt
for n, grp in iris.groupby("species"):
plt.scatter(grp.petal_length, grp.petal_width, label=n)
plt.legend()
plt.show()
An easy solution is also to use seaborn.
import seaborn as sns
iris = sns.load_dataset("iris")
import matplotlib.pyplot as plt
g = sns.FacetGrid(iris, hue="species")
g.map(plt.scatter, 'petal_length','petal_width').add_legend()
plt.show()

seaborn pairplot seperate bins in diagonal

sns.pairplot(iris, hue='class', palette='husl',kind='reg')
plt.show()
That is the code I used to generate the plot(picture below).
There is also a picture of how I want it to look.
This is how it looks now:
This is how I want it to look:
The main difference is that compared to the example you show on the bottom, you use a different palette and a regression line.
Once you leave out palette='husl',kind='reg' you get the plot on the bottom.
The remaining difference is due to a different style in different matplotlib versions. The picture on the bottom has been produced with a version of matplotlib prior to version 2. To get the old style back in matplotlib 2 you can use
import matplotlib.pyplot as plt
plt.style.use('classic')
Complete code:
import matplotlib.pyplot as plt
plt.style.use('classic')
import seaborn as sns; sns.set(style="ticks", color_codes=True)
iris = sns.load_dataset("iris")
g = sns.pairplot(iris, hue="species")
plt.show()
produces
import seaborn as sns;
iris = sns.load_dataset("iris")
sns.pairplot(iris, hue='class')

Categories

Resources