I want to fill the area under a line plot so it looks as the picture below:
instead of
built on the following .csv file:
01-01-97 1
01-02-97 2
01-03-97 3
...
01-11-17 251
01-12-17 252
01-01-18 253
what should I change in this code to generate the desired graph?
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
# load csv
df=pd.read_csv("test.csv")
# generate graph
g = sns.lineplot(x="Date", y="Data", data=df)
plt.show()
plt.fill_between(df.Date.values, df.Data.values)
Here's an alternative, using a stacked line chart:
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
# load csv
df = pd.read_csv("test.csv")
# generate graph
plt.stackplot(df["Date"], df["Data"], alpha=0.5)
plt.show()
Related
When rendering matplotlib charts using pyscript and using figure(figsize) the chart disappears and replaces the x and y axes. Why is this happening?
With figsize
Without figsize
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from pyodide.http import open_url
url = open_url('../data/salaries.csv')
df = pd.read_csv(url)
df_no_outliers=df[(df.salary_in_usd>min) & (df.salary_in_usd<max)]
top_jobs=df_no_outliers.job_title.value_counts()[:7]
plt.figure(2)
barplot = sns.barplot(x='work_year', y='salary_in_usd', data=df_no_outliers)
plt.tight_layout()
plt.figure(figsize=(15,8))
plt
I have to chart a data from csv somewhere from my directory. I am using python by learning some samples online. Problem is, I can't find any solution to show all x-axis labels.
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
pathcsv = r'D:\iPython\csvfile\samplecsv2.csv'
df = pd.read_csv(pathcsv)
df.set_index('Names').plot()
plt.show()
you can do that by using set_xticklabels to set the names and set_xticks to show ticks for each country. Updated code is below...
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
pathcsv = r'D:\iPython\csvfile\samplecsv2.csv'
ax =df.set_index('Names').plot()
ax.set_xticks(np.arange(len(df))) #Show ticks for each country
ax.set_xticklabels(df.Names) #Show labels as in df.Names
plt.show()
Output graph
I'm having trouble creating this plot in spyder:
import seaborn as sns
import pandas as pd
from pandas.api.types import CategoricalDtype
diamonds= sns.load_dataset("diamonds")
df=diamonds.copy()
cut_Kategoriler=["Fair","Good","Very Good","Premium","Ideal"]
df.cut=df.cut.astype(CategoricalDtype(categories = cut_Kategoriler,ordered=True))
print(df.head())
sns.catplot(x="cut",y="price",data=df)
sns.barplot(x="cut",y="price",hue="color",data=df)
I want create two plots. But these plots overflap. How can i separate the graphics in the last two lines?
You need to import matplotlib.pyplot as plt and then add plt.show() after each of the two plots.
The modified code is added below:
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt # Import Matplotlib
from pandas.api.types import CategoricalDtype
diamonds = sns.load_dataset("diamonds")
df=diamonds.copy()
cut_Kategoriler=["Fair","Good","Very Good","Premium","Ideal"]
df.cut=df.cut.astype(CategoricalDtype(categories = cut_Kategoriler,ordered=True))
print(df.head())
sns.catplot(x="cut",y="price",data=df)
plt.show() # Display the first plot
sns.barplot(x="cut",y="price",hue="color",data=df)
plt.show() # Display the second plot
Code is below
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.cluster import KMeans
import seaborn as sns
df = pd.DataFrame(np.random.rand(10,3), columns=["A", "B","C"])
km = KMeans(n_clusters=3).fit(df)
df['cluster_id'] = km.labels_
test = {0:"Blue", 1:"Red", 2:"Green"}
#sns.scatterplot()
plt.show()
I am trying to plot without x,y that is column constraints. I need to plot any number of columns just want to plot the cluster graph
I have a scatter plot im working with and for some reason im not seeing all the x values on my graph
#%%
from pandas import DataFrame, read_csv
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
file = r"re2.csv"
df = pd.read_csv(file)
#sns.set(rc={'figure.figsize':(11.7,8.27)})
g = sns.FacetGrid(df, col='city')
g.map(plt.scatter, 'type', 'price').add_legend()
This is an image of a small subset of my plots, you can see that Res is displaying, the middle bar should be displaying Con and the last would be Mlt. These are all defined in the type column from my data set but are not displaying.
Any clue how to fix?
Python is doing what you tell it to do. Just pick different features, presumably things that make more sense for plotting, if you want to generate a more interesting plots. See this generic example below.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme(style="darkgrid")
tips = sns.load_dataset("tips")
sns.relplot(x="total_bill", y="tip", hue="smoker", data=tips);
Personally, I like plotly plots, which are dynamic, more than I like seaborn plots.
https://plotly.com/python/line-and-scatter/