Pandas plot gives mismatched x-axis with secondary_y=True - python

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)

Related

How to plot boundary lines for max and min values on each plot in python seaborn?

suppose I am plotting scatter plot in seaborn python
import seaborn as sns
df = sns.load_dataset("penguins")
sns.scatterplot(data=df, x="flipper_length_mm", y="body_mass_g",hue="sex",s=300)
What I want is boundary lines of max and min values in each scale.
The question might be hard to understand. If you didn't understand you can tell me
You'll need to calculate your min and max values for each axis. You can use the panda DataFrame methods .min() and .max() for that respectively.
To add them as lines to your scatterplot you can use the matplotlib.pyplot methods, .axhline() and .axvline() for your horizontal/y-axis and vertical/x-axis.
For example adding a horizontal min line:
import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset("penguins")
graph = sns.scatterplot(data=df, x="flipper_length_mm", y="body_mass_g",hue="sex",s=300)
graph.axhline(df['body_mass_g'].min())

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')

Seaborn plots clashes

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())

How to plot only one half of a scatter matrix using pandas

I am using pandas scatter_matrix (couldn't get PairgGrid in seaborn to work) to plot all combinations of a set of columns in a pandas frame. Each column as 1000 data points and there are nine columns.
I am using the following code:
pandas.plotting.scatter_matrix(df, alpha=0.2, figsize=(8,8))
I get the figure shown below:
This is nice., However, you'll notice that across the main diagonal I have a mirror image. Is it possible to plot only the lower portion as in the following fake plot I made using paint:
This is probably not the cleanest way to do it, but it works:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
axes = pd.plotting.scatter_matrix(iris, alpha=0.2, figsize=(8,8))
for i in range(np.shape(axes)[0]):
for j in range(np.shape(axes)[1]):
if i < j:
axes[i,j].set_visible(False)

Plot pandas dataframe with varying number of columns along imshow

I want to plot an image and a pandas bar plot side by side in an iPython notebook. This is part of a function so that the dataframe containing the values for the bar chart can vary with respect to number of columns.
The libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline
Dataframe
faces = pd.Dataframe(...) # return values for 8 characteristics
This returns the the bar chart I'm looking for and works for a varying number of columns.
faces.plot(kind='bar').set_xticklabels(result[0]['scores'].keys())
But I didn't find a way to plot it in a pyplot figure also containing the image. This is what I tried:
fig, (ax_l, ax_r) = plt.subplots(nrows=1, ncols=2, figsize=(15, 5))
ax_l.imshow( img )
ax_r=faces.plot(kind='bar').set_xticklabels(result[0]['scores'].keys())
The output i get is the image on the left and an empty plot area with the correct plot below. There is
ax_r.bar(...)
but I couldn't find a way around having to define the columns to be plotted.
You just need to specify your axes object in your DataFrame.plot calls.
In other words: faces.plot(kind='bar', ax=ax_r)

Categories

Resources