Plotting scatter graph with marginal histograms with Python seaborn 0.7 - python

I have some python code that plotted a scatter graph with marginal histograms. This worked fine with seaborn 0.5 and still does work if I go back to this version. However, I'd like to get it to work with 0.7!
I don't use python often and mainly only use scripts set up by others that I adjust slightly.
The problem appears to be happening in the following line:
sns.lmplot(x='Moisture Content (%)', y='Dry Density (kg/m3)', hue='Test',
data=data, ax=ax_joint, fit_reg=False, legend=False, palette=test_colours)
ax is no longer recognised in seaborn 0.7.
TypeError: lmplot() got an unexpected keyword argument 'ax'
The output still gives me completed marginal histograms but the scatter graph is blank.
If you need more info then please let me know.
Cheers

seaborn lmplot ax will show you that lmplot returns you a figure-level instead of axes-level. So in order to manage lmplot use FacetGrid.
FacetGrid will plot on different variables that you states in different graphs.
import matplotlib.pyplot as plt
g = sns.FacetGrid(tips, col="time", row="smoker")
g = g.map(plt.hist, "total_bill")
FacetGrid result image
You can imagine doing a groupby on the dataframe which gives you the different graphs.
Refer to this for more example on plotting lmplot

Related

Plotting pandas vs matplotlib

For a little jupyter notebook widget i tried the following:
with output:
fig, ax = plt.subplots(constrained_layout=True, figsize=(6, 4))
# move the toolbar to the bottom
fig.canvas.toolbar_position = 'bottom'
ax.grid(True)
line, = ax.plot(df.index, init_data, initial_color,)
ax.set_ylabel('CH4_HYD1')
ax.set_ylabel('')
But the data gets plotted in a wierd way. When isolating the plotting method and comparing it to the pandas default, the pandas plot displays the data correctly.
df['CH4_HYD1'].plot()
Pandas plot
plt.plot(['CH4_HYD1'])
Wrong plot?
I would like to use the axis plot method to produce a plot with multiple lines of different features. But I can't find the probably very simple error... What is the difference here?
This is a short example of how to plot multiple lines from a DataFrame with matplotlib and with pandas. Essentially, pandas uses matplotlib to do the plots so it shouldn't be different.
Anyway, I encourage you to look into seaborn. This is a great library based on pandas and matplotlib that enables visualizing dataframes easily.

Multiple Scatter plotting in matplotlib

i wanna make multiple scatter plot using matplotlib how can I subplots the scatter what I mean is to make len(features) plot scatter, I've tried this but didn't succeed
for each in featureset:
i=0
fig,ax=plt.subplot(len(features))
ax[i].scatter(df[each],df[Target],color='Blue')
i+=1
so i solved my problem from myself here the solution
import seaborn as sns
features=df.columns[:len(df.columns)-1]
target=df.columns[len(df.columns)-1]
sns.pairplot(df,x_vars=features,y_vars=target)
that's it, a very easy solution

Make Python seaborn heatmap bigger

I use a heat map in Python to show the correlation between all parameters I have. The number of parameters however are that large that the heat map becomes to small to show the data.
Heat Map
The heat map is created using seaborn:
seaborn.heatmap(df.corr())
I tried to make it bigger using:
plt.subplots(figsize=(10,10))
seaborn.heatmap(df.corr())
but this didn't work since the image just remained its current size.
Does someone know another way of doing this? Or maybe another way to clearly plot the correlations between all parameters?
Regards, Ganesh
You should create the figure first (similar to how you tried) using:
fig, ax = plt.subplots(figsize=(10,10))
Then, pass in ax as an argument to seaborn.heatmap
import matplotlib.pyplot as plt
import seaborn
fig, ax = plt.subplots(figsize=(10,10))
seaborn.heatmap(df.corr(), ax=ax)
plt.show()

Pyplot/Subplot APIs Matplotlib

I'm making something using Matplotlib where I have multiple subplots on a figure. It seems to me like the subplot API is limited compared to the PyPlot API: for example, I can't seem to make custom axes labels in my subplot although it is possible using PyPlot.
My question is: Is there a richer subplot API besides the tiny one on the PyPlot page (http://matplotlib.org/api/pyplot_api.html), and/or is there a way to get the full functionality of a PyPlot on a subplot?
Basically, what is a subplot? I can't find it in the documentation. Even more generally, when should I use a figure vs an axis vs a subplot? They all seem to do essentially the same thing.
Consider the following code:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(2,1,1)
Then ax is an axis? Can I use the pyplot API to customize ax?
Thanks for your help.
While i suggest that use the axes methods, there is the plt.sca function (set current axes).
So
plt.sca(ax)
does what you want, i think.

show two plot together in Matplotlib like show(fig1,fig2)

In general, I don't have any problem to put two plots in a figure like plot(a);plot(b) in matplotlib. Now I am using a particular library which would generate a figure and I want to overlay with boxplot. Both are generated by matplotlib. So I think it should be fine but I can only see one plot. Here is the code. I am using beeswarm and here is its ipython notebook. I can only plot beeswarm or boxplot but not both in a figure. My main goal is trying to save column scatter plot and boxplot together as a figure in pdf. Thanks,
from beeswarm import beeswarm
fig=plt.figure()
figure(figsize=(5,7))
ax1=plt.subplot(111)
fig.ylim=(0,11)
d2 = np.random.random_integers(10,size=100)
beeswarm(d2,col="red",method="swarm",ax=ax1,ylim=(0,11))
boxplot(d2)
The problem is with the positioning of the box plot. The default positioning list starts with 1 what shifts the plot to 1 and your beeswarm plot sits on 0.
So the plots are in different places of your canvas.
I modified your code a little bit and that seems to solve your problem.
from beeswarm import beeswarm
fig = plt.figure(figsize=(5,7))
ax1 = fig.add_subplot(111)
# Here you may want to use ax1.set_ylim(0,11) instead fig.ylim=(0,11)
ax1.set_ylim(0,11)
d2 = np.random.random_integers(10,size=100)
beeswarm(d2,col="red",method="swarm",ax=ax1,ylim=(0,11))
boxplot(d2,positions=[0])
Cheers

Categories

Resources