Line plot is filled in Pandas and Matplotlib - pandas

I am using pandas and matplotlib to plot a data set.
I am trying to plot two values in a DataFrame a a line plot, but The plot is displaying filled.
but I want this:
my code:
%matplotlib inline
from matplotlib import style
import pandas as pd
from matplotlib import pyplot as plt
covid = pd.read_csv('covid_19_india.csv')
#covid.head()
plt.style.use('Solarize_Light2')
plt.plot(covid.Date, covid.Deaths)
Help needed from community!

Related

Empty Plots - matplotlib only shows frame of plot but no data

From my excel imported file, I want to plot specific entries i.e.rows and columns but plt.plot command does not display the data, only a blank frame is shown. please see the attached picture.
May be it has something to do with my code.
%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
hpge = pd.read_excel('mypath\\filename.xlsx','Sheet3', skiprows=1,usecols='C:G,I,J')
x=[]
y=[]
x.append(hpge.E_KeV[2700:2900])# E_KeV is a column
y.append(hpge.Fcounts[2700:2900])# Fcounts is a column
x1=[]
y1=[]
x1.append(hpge.E[2700:2900])
y1.append(hpge.C[2700:2900])
#print(y1)
#print(x)
#plt.xlim(590,710)
#plt.yscale('log')
plt.plot(x, y, label='Cs')
plt.plot(x1,y1)
plt.show()

python pandas plot line chart in pandas.plot hbar

I have a horizontal bar chart created with
df.plot(kind='barh', ax=ax)
and now I would like to plot a horizontal line chart in the same axis. How can I do that. There seems to be no equivalent lineh
I tried to just flip axes when plotting a regular line
df=pd.DataFrame(dict(k=['A','B','C','D'], v=[1,3,2,3]))
df.plot(x='v', y='k')
but then pandas complains that there is no numerical data to plot
If you want to use matplotlib, you can do like the following. Here the command xticks() is to set x-tick labels only at integer values.
import pandas as pd
import matplotlib.pyplot as plt
df=pd.DataFrame(dict(k=['A','B','C','D'], v=[1,3,2,3]))
plt.plot(df.v, df.k)
plt.xticks(range(1, max(df.v)+1))
plt.show()

Seaborn y labels are overlapping

So I tried to make a categorical plot of my data and this is what my code and the graph.
import pandas as pd
import numpy as np
import matplotlib as plt
import seaborn as sns
sns.set(style="whitegrid")
sns.set_style("ticks")
sns.set_context("paper", font_scale=1, rc={"lines.linewidth": 6})
sns.catplot(y = "Region",x = "Interest by subregion",data = sample)
Image:
How can I make the y-labels more spread out and have a bigger font?
Try using sns.figure(figsize(x,y)) and sns.set_context(context=None,font_scale=1).
Try different values for these parameters to get the best results.

Define hues by column values in seaborn barplot

I would like the colour of the columns to be determined by their value on the x-axis, e.g. bars with identical values on the x-axis should have identical colours assigned to them.
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.DataFrame(index=['A','B','C','D','E','F'],data={'col1':np.array([2.3423,4.435,9.234,9.234,2.456,6.435])})
ax = sns.barplot(x='col1', y=df.index.values, data=df,palette='magma')
This is what it looks like at the moment with default settings. I presume there is a simple elegant way of doing this, but interested in any solution.
Here a solution:
import seaborn as sns
import matplotlib as mpl, matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.DataFrame(index=['A','B','C','D','E','F'],
data={'col1':np.array([2.3423,4.435,9.234,9.234,2.456,6.435])})
ax = sns.barplot(x='col1', y=df.index.values, data=df,
palette=mpl.cm.magma(df['col1']*.1))
Note: mpl.cm.magma is a Colormap instance and is used to convert data values (floats) from the interval [0, 1] to colors that the Colormap represents. If you want "auto scaling" of your data values, you could use palette=mpl.cm.ScalarMappable(cmap='magma').to_rgba(df['col1']) instead in the sns.barplot() call.
Here the output:

prevent overlapping bars using seaborn with pandas plotting

I am trying to use pandas plotting to create a stacked horizontal barplot with a seaborn import. I would like to remove space between the bars, but also not have the bars overlap. This is what I've tried:
import pandas as pd
import numpy as pd
import seaborn as sns
df = pd.DataFrame(np.random.rand(15, 3))
df.plot.barh(stacked=True, width=1)
This seems to work without importing seaborn, though I like the seaborn style and it is usually an import in the ipython notebook I am working in is this possible?
This artifact is also visible with matplotlib defaults if you set the bar linewidth to what seaborn style has:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(15, 3))
df.plot(stacked=True, width=1, kind="barh", lw=.5)
A solution would be to increase the bar lines back to roughly where the matplotlib defaults are:
import pandas as pd
import numpy as np
import seaborn as sns
df = pd.DataFrame(np.random.rand(15, 3))
df.plot(stacked=True, width=1, kind="barh", lw=1)
Perhaps you should reduce the line width?
import seaborn as sns
f, ax = plt.subplots(figsize=(10, 10))
df.plot(kind='barh', stacked=True, width=1, lw=0.1, ax=ax)