Plotting different pandas dataframes in one figure [duplicate] - python

This question already has answers here:
How to plot in multiple subplots
(12 answers)
How to plot multiple dataframes in subplots
(10 answers)
Plotting Pandas into subplots
(1 answer)
Plot two pandas data frames side by side, each in subplot style
(1 answer)
Closed 1 year ago.
I'm trying to plot different dataframes in a plot (each dataframe is a subplot).
I'm doing:
plt.figure(figsize=(20,20))
for i,name in enumerate(names):
df = pd.read_excel(myPath+name)
df['Datetime'] = pd.to_datetime(df['Datetime'])
df = df.set_index('Datetime')
plt.subplot(4, 1, i+1) # I have 4 dataframes
df.plot()
plt.show()
But I obtain this...
How can I do it correctly?
Thank you!

df.plot doesn't integrate too well in more complex plotting patterns. It's meant more as a shortcut to get a quick plot from your Dataframe while doing data exploration.
Thus, you end up creating 4 empty subplots and 4 df.plot(). I imagine that the first 3 df.plot results plots are overridden by the last one. This is probably due to the figure/axes handling of df.plot which is outside the scope of this question.
Try something like this instead.
fig, axes = plt.subplots(4, 1)
for i,name in enumerate(names):
df = pd.read_excel(myPath+name)
df['Datetime'] = pd.to_datetime(df['Datetime'])
df = df.set_index('Datetime')
axes[i].plot(df)
plt.show()
For more information on proper use of subplots, have a look at these examples: https://matplotlib.org/3.1.0/gallery/subplots_axes_and_figures/subplots_demo.html
EDIT: I had misread your code and my previous comments about specifying the keys were probably erroneous. I have updated my answer now. Hope it works for you

Related

Python line plotting 2 columns in same DataFrame using Index and count [duplicate]

This question already has answers here:
How to plot multiple pandas columns
(3 answers)
Plot multiple columns of pandas DataFrame using Seaborn
(2 answers)
How do I create a multiline plot using seaborn?
(3 answers)
Closed 26 days ago.
Newbie to Python so am unsure whether this can be done in one graph or not. I have one DataFrame containing Year, Number of Accidents and Number of Fatalities:
I am trying to generate a line plot that shows x axis = Year, y axis = number of instances per year, and 2 lines showing number of each individual column. Using Seaborn, I can only see a way to map 2 columns and hue. Can anyone please provide any advice on whether this is achievable in either Matplotlib or Seaborn.
Tried using Seaborn but cannot work out how to set up x and y axis as required and show 2 individual columns within that:
sns.lineplot(x=f1_safety['NumberOfFatalities'],y=f1_safety['NumberOfAccidents'].count(), hue = f1_safety['year'].count())
plt.show()
There are at least two ways to accomplish what you want to do here.
The simpler one uses pandas built-in plotting API. You can plot dataframes directly when they are already in the correct form. In your case, you need to set the year as the index, and then can plot right away:f1_safety.set_index("year").plot()
If you want to use seaborn, you first need to transform the data into the correct format. seaborn takes x and y, and you can not specify different y columns directly (like y1, y2 and so on). Instead, you need to transform the data into "long format". In such a table, you get one index or id column, one value column and a "description" kind of columns. This works like this:
f1_safety = pd.melt(df, id_vars="year", value_vars=["NumberOfAccidents", "NumberOfFatalities"])
sns.lineplot(data=f1_safety, x="year", y="value", hue="variable")
The plot in both cases looks quite the same:
There are other ways. In particular, in Jupyter you can execute two plot statements in the same cell, and matplotlib will put the plots into the same figure, even cycling through the colors as necessary.

Prescribing axes to the figure in matplotlib [duplicate]

This question already has an answer here:
How to return a matplotlib.figure.Figure object from Pandas plot function
(1 answer)
Closed 2 years ago.
I have created a 3x3 subplots by using the pandas df.line.plot:
ax=df.plot.line(subplots=True, grid=True,layout=(3, 3), sharex=True, legend=False,ylim=[-5,25])
It returns 3x3 matrix of Axes objects.
Now I want to create a joint legend for those subplots.
As the other post suggests I should use:
handles, labels = ax.get_legend_handles_labels()
fig.legend(handles, labels, loc='upper center')
The problem is I can't use it because I have no figure created here. I only have axes. How can I make it work?
I edited the post, because I thought I could create a figure and prescribe the axes, but I guess it came from my confusion on subject.
You have two options:
First: Either use double indices (for row and column) as shown in the comments and then use ax[0,0], ax[0,1], ax[0,2] ... ax[2,0], ax[2,1], ax[2,2]. For 3 rows and 3 columns, the indices will run from 0 up to 2 (so 0, 1, 2)
You can also use ax[0][0] and so on. Both formats are equivalent.
Second: If you don't want to use two indices, you can flatten the ax and then use a single index as
ax = ax.flatten()
This will convert ax from 2d object to a 1-d array of 9 subfigures. Then you can use ax[0], ax[1], ax[2], ... ax[8] (9-1 because indexing starts from 0 in python)

pandas color scheme not working properly with my data (python) [duplicate]

This question already has answers here:
Pandas DataFrame Bar Plot - Plot Bars Different Colors From Specific Colormap
(3 answers)
Closed 4 years ago.
I would like to change the default color scheme of my pandas plot. I tried with different color schemes through cmap pandas parameter, but when I change it, all bars of my barplot get the same color.
The code I tried is the following one:
yearlySalesGenre = df1.groupby('Genre').Global_Sales.sum().sort_values()
fig = plt.figure()
ax2 = plt.subplot()
yearlySalesGenre.plot(kind='bar',ax=ax2, sort_columns=True, cmap='tab20')
plt.show(fig)
And the data that I plot (yearlySalesGenre) is a pandas Series type:
Genre
Strategy 174.50
Adventure 237.69
Puzzle 243.02
Simulation 390.42
Fighting 447.48
Racing 728.90
Misc 803.18
Platform 828.08
Role-Playing 934.40
Shooter 1052.94
Sports 1249.47
Action 1745.27
Using tab20 cmap I get the following plot:
I get all bars with the first color of all the tab20 scheme. What I am doing wrong?
Note that if I use the default color scheme of pandas plot, it properly displays all bars with different colors, but the thing is that I want to use a particular color scheme.
As posted, it's a duplicated answer. Just in case, the answer is that pandas makes color schemes based on different columns, not in rows. So to use different colors you can transpose the data + some other stuff (duplicated link), or directly use the matplotlib.pyplot plotting that allows more flexibility (in my case):
plt.bar(range(len(df)), df, color=plt.cm.tab20(np.arange(len(df))))
Maybe this is what you want:
df2.T.plot( kind='bar', sort_columns=True, cmap='tab20')
I think the problem you have is that you only have one series. Pandas plot bar will plot separate series (columns) each with its own color, and separate each each bar based on the index.
By using .T, the series in your data become multiple columns but within only one index. I am sure you can play with the legend to get a better display.

Asymmetrical errorbar with pandas [duplicate]

This question already has an answer here:
Why is pandas applying the same values on both sides of an asymmetric error bar?
(1 answer)
Closed 5 years ago.
I want to plot asymmetrical errorbars with pandas. According to official docs this should work
df = pd.DataFrame([[1,0.2,0.7]])
fig, ax = plt.subplots()
df[0].plot.bar(yerr=[df[1], df[2]], ax=ax)
But pandas renders errorbar as df[1] for both lower and upper limits (-0.2/+0.2 istead of -0.2/+0.7):
Where do I make a mistake?
I use pandas v0.20.3 with python v2.7.13 under Windows 7.
Your yerr is 1D:
yerr=[df[1], df[2]]
It needs to be 2D, specifically one row per data point and each row having two values for negative and positive error:
yerr=[[df[1], df[2]]]

Plotting two numeric columns in pandas in same graph with dates on x-axis [duplicate]

This question already has answers here:
How to plot different groups of data from a dataframe into a single figure
(5 answers)
Closed 5 years ago.
From this code:
data.Date = pd.to_datetime(data.Date)
data.sort_values('Date', inplace=True)
data['mean_Kincaid'] = data.Kincaid.rolling(250,
min_periods=1).mean()
data.plot(x='Date', y='mean_Kincaid',
legend=False, title="Kincaid scores over time")
Using this line:
data['mean_Score'] = data.Score.rolling(250, min_periods=1).mean()
I would like to plot the 'mean_Score' on the same graph preferably with a dotted line. My attempt:
data.plot(x='Date', y='mean_Kincaid', y = 'mean_Score', legend=False,
title="Kincaid scores over time")
Use get the axes handle from the first plot then use the ax paramater in pandas.DataFrame.plot to plot second line on same axes:
ax = data.plot(x='Data', y='mean_Kincaid',legend=False, title="Kincaid score over time")
data.plot(x='Date', y='mean_Score', ax=ax)

Categories

Resources