Multiple data frame columns plotted in the same bar without overlapping The Next CEO of Stack OverflowSelecting multiple columns in a pandas dataframeChange data type of columns in PandasPython: Seaborn Getting Hex values of a color palette (or color palette is not working with scatter)plotting multiple columns value in x-axis in pythonmatplotlib: plot multiple columns of pandas data frame on the bar chartMatplotlib bar plot order not alphabeticalPandas / matplotlib is showing 2018 and 2019 years as 48 and 49ggplotly plotting multiple timeseries as barchart with dynamicTicks = THow to plot a bar graph of days of the week on top of a line graph of different values at different dates pythonHow to plot line and bar-chart on the same x-axis (datetime) but different y-axis with pyplot?
Lucky Feat: How can "more than one creature spend a luck point to influence the outcome of a roll"?
Expressing the idea of having a very busy time
How to Implement Deterministic Encryption Safely in .NET
Vector calculus integration identity problem
Is a distribution that is normal, but highly skewed, considered Gaussian?
Is it correct to say moon starry nights?
Audio Conversion With ADS1243
Is there such a thing as a proper verb, like a proper noun?
Help/tips for a first time writer?
How to avoid supervisors with prejudiced views?
IC has pull-down resistors on SMBus lines?
Is Nisuin Biblical or Rabbinic?
What flight has the highest ratio of timezone difference to flight time?
It is correct to match light sources with the same color temperature?
Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?
Purpose of level-shifter with same in and out voltages
Is it convenient to ask the journal's editor for two additional days to complete a review?
Is there a difference between "Fahrstuhl" and "Aufzug"?
What would be the main consequences for a country leaving the WTO?
Is there a way to save my career from absolute disaster?
Can I board the first leg of the flight without having final country's visa?
Does higher Oxidation/ reduction potential translate to higher energy storage in battery?
Is there a reasonable and studied concept of reduction between regular languages?
Computationally populating tables with probability data
Multiple data frame columns plotted in the same bar without overlapping
The Next CEO of Stack OverflowSelecting multiple columns in a pandas dataframeChange data type of columns in PandasPython: Seaborn Getting Hex values of a color palette (or color palette is not working with scatter)plotting multiple columns value in x-axis in pythonmatplotlib: plot multiple columns of pandas data frame on the bar chartMatplotlib bar plot order not alphabeticalPandas / matplotlib is showing 2018 and 2019 years as 48 and 49ggplotly plotting multiple timeseries as barchart with dynamicTicks = THow to plot a bar graph of days of the week on top of a line graph of different values at different dates pythonHow to plot line and bar-chart on the same x-axis (datetime) but different y-axis with pyplot?
I have a pandas dataframe:
import pandas as pd
data1 = 'Date':['03-19-2019'],
'Total':[35],
'Solved':[19],
'Arrived':[23],
df1 = pd.DataFrame(data1)
and I want to plot a bar plot like this:
with
df1.plot(kind='barh',x='Date',y='Total', ax=ax0, color='#C0C0C0',
width=0.5)
df1.plot(kind='barh',x='Date',y='Arrived', ax=ax0, color='#C0FFFF',
width=0.5)
df1.plot(kind='barh',x='Date',y='Solved', ax=ax0, color='#C0C0FF',
width=0.5)
However, to avoid overlapping, I have to draw each column taking into account which of them has the bigger value.(Total greater than Arrived greater than Solved)
How can I avoid to do this and automate this process easily?
python matplotlib bar-chart
add a comment |
I have a pandas dataframe:
import pandas as pd
data1 = 'Date':['03-19-2019'],
'Total':[35],
'Solved':[19],
'Arrived':[23],
df1 = pd.DataFrame(data1)
and I want to plot a bar plot like this:
with
df1.plot(kind='barh',x='Date',y='Total', ax=ax0, color='#C0C0C0',
width=0.5)
df1.plot(kind='barh',x='Date',y='Arrived', ax=ax0, color='#C0FFFF',
width=0.5)
df1.plot(kind='barh',x='Date',y='Solved', ax=ax0, color='#C0C0FF',
width=0.5)
However, to avoid overlapping, I have to draw each column taking into account which of them has the bigger value.(Total greater than Arrived greater than Solved)
How can I avoid to do this and automate this process easily?
python matplotlib bar-chart
add a comment |
I have a pandas dataframe:
import pandas as pd
data1 = 'Date':['03-19-2019'],
'Total':[35],
'Solved':[19],
'Arrived':[23],
df1 = pd.DataFrame(data1)
and I want to plot a bar plot like this:
with
df1.plot(kind='barh',x='Date',y='Total', ax=ax0, color='#C0C0C0',
width=0.5)
df1.plot(kind='barh',x='Date',y='Arrived', ax=ax0, color='#C0FFFF',
width=0.5)
df1.plot(kind='barh',x='Date',y='Solved', ax=ax0, color='#C0C0FF',
width=0.5)
However, to avoid overlapping, I have to draw each column taking into account which of them has the bigger value.(Total greater than Arrived greater than Solved)
How can I avoid to do this and automate this process easily?
python matplotlib bar-chart
I have a pandas dataframe:
import pandas as pd
data1 = 'Date':['03-19-2019'],
'Total':[35],
'Solved':[19],
'Arrived':[23],
df1 = pd.DataFrame(data1)
and I want to plot a bar plot like this:
with
df1.plot(kind='barh',x='Date',y='Total', ax=ax0, color='#C0C0C0',
width=0.5)
df1.plot(kind='barh',x='Date',y='Arrived', ax=ax0, color='#C0FFFF',
width=0.5)
df1.plot(kind='barh',x='Date',y='Solved', ax=ax0, color='#C0C0FF',
width=0.5)
However, to avoid overlapping, I have to draw each column taking into account which of them has the bigger value.(Total greater than Arrived greater than Solved)
How can I avoid to do this and automate this process easily?
python matplotlib bar-chart
python matplotlib bar-chart
edited Mar 21 at 19:22
Bazingaa
15.3k21330
15.3k21330
asked Mar 21 at 19:03
LauraLaura
190113
190113
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
There must be a straightforward and simpler approach in Pandas but I just came up with this quick workaround. The idea is following:
- Leave out the first column
Date
and sort the remaining columns. - Use the sorted indices for plotting the columns in ascending order
- To make the colors consistent, you can make use of dictionary so that the ascending/descending order doesn't affect your colors.
fig, ax0 = plt.subplots()
ids = np.argsort(df1.values[0][1:])[::-1]
colors = 'Total': '#C0C0C0', 'Arrived': '#C0FFFF', 'Solved':'#C0C0FF'
for col in np.array(df1.columns[1:].tolist())[ids]:
df1.plot(kind='barh',x='Date',y=col, ax=ax0, color=colors[col], width=0.1)
thanks! the only disadvantage I can see with this approach is that the colors dont represent always the same type of data
– Laura
Mar 21 at 20:09
@Laura: I edited my solution to make the colors represent always the same items using a dictionary
– Bazingaa
Mar 21 at 20:15
Thanks this is exactly what I was looking for
– Laura
Mar 22 at 17:28
add a comment |
A stacked bar graph can be produced in pandas via the stacked=True
option. To use this you need to make the "Date"
the index first.
import matplotlib.pyplot as plt
import pandas as pd
data1 = 'Date':['03-19-2019'],
'Total':[35],
'Solved':[19],
'Arrived':[23],
df = pd.DataFrame(data1)
df.set_index("Date").plot(kind="barh", stacked=True)
plt.show()
Comparing your figure with OP's figure in the question, it seems he/she doesn't want a stacked bar chart but all the bars to start at x=0 but zorder arranged such that the lowest bar is at the front and the highest at the back. Compare the numbers on the x-axis
– Bazingaa
Mar 22 at 10:38
Thanks @ImportanceOfBeingErnest , Bazingaa is right. I wanted all bars start as 0. I have solved now taking into account the solution provided by him/her. Thanks
– Laura
Mar 22 at 17:30
add a comment |
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55287582%2fmultiple-data-frame-columns-plotted-in-the-same-bar-without-overlapping%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There must be a straightforward and simpler approach in Pandas but I just came up with this quick workaround. The idea is following:
- Leave out the first column
Date
and sort the remaining columns. - Use the sorted indices for plotting the columns in ascending order
- To make the colors consistent, you can make use of dictionary so that the ascending/descending order doesn't affect your colors.
fig, ax0 = plt.subplots()
ids = np.argsort(df1.values[0][1:])[::-1]
colors = 'Total': '#C0C0C0', 'Arrived': '#C0FFFF', 'Solved':'#C0C0FF'
for col in np.array(df1.columns[1:].tolist())[ids]:
df1.plot(kind='barh',x='Date',y=col, ax=ax0, color=colors[col], width=0.1)
thanks! the only disadvantage I can see with this approach is that the colors dont represent always the same type of data
– Laura
Mar 21 at 20:09
@Laura: I edited my solution to make the colors represent always the same items using a dictionary
– Bazingaa
Mar 21 at 20:15
Thanks this is exactly what I was looking for
– Laura
Mar 22 at 17:28
add a comment |
There must be a straightforward and simpler approach in Pandas but I just came up with this quick workaround. The idea is following:
- Leave out the first column
Date
and sort the remaining columns. - Use the sorted indices for plotting the columns in ascending order
- To make the colors consistent, you can make use of dictionary so that the ascending/descending order doesn't affect your colors.
fig, ax0 = plt.subplots()
ids = np.argsort(df1.values[0][1:])[::-1]
colors = 'Total': '#C0C0C0', 'Arrived': '#C0FFFF', 'Solved':'#C0C0FF'
for col in np.array(df1.columns[1:].tolist())[ids]:
df1.plot(kind='barh',x='Date',y=col, ax=ax0, color=colors[col], width=0.1)
thanks! the only disadvantage I can see with this approach is that the colors dont represent always the same type of data
– Laura
Mar 21 at 20:09
@Laura: I edited my solution to make the colors represent always the same items using a dictionary
– Bazingaa
Mar 21 at 20:15
Thanks this is exactly what I was looking for
– Laura
Mar 22 at 17:28
add a comment |
There must be a straightforward and simpler approach in Pandas but I just came up with this quick workaround. The idea is following:
- Leave out the first column
Date
and sort the remaining columns. - Use the sorted indices for plotting the columns in ascending order
- To make the colors consistent, you can make use of dictionary so that the ascending/descending order doesn't affect your colors.
fig, ax0 = plt.subplots()
ids = np.argsort(df1.values[0][1:])[::-1]
colors = 'Total': '#C0C0C0', 'Arrived': '#C0FFFF', 'Solved':'#C0C0FF'
for col in np.array(df1.columns[1:].tolist())[ids]:
df1.plot(kind='barh',x='Date',y=col, ax=ax0, color=colors[col], width=0.1)
There must be a straightforward and simpler approach in Pandas but I just came up with this quick workaround. The idea is following:
- Leave out the first column
Date
and sort the remaining columns. - Use the sorted indices for plotting the columns in ascending order
- To make the colors consistent, you can make use of dictionary so that the ascending/descending order doesn't affect your colors.
fig, ax0 = plt.subplots()
ids = np.argsort(df1.values[0][1:])[::-1]
colors = 'Total': '#C0C0C0', 'Arrived': '#C0FFFF', 'Solved':'#C0C0FF'
for col in np.array(df1.columns[1:].tolist())[ids]:
df1.plot(kind='barh',x='Date',y=col, ax=ax0, color=colors[col], width=0.1)
edited Mar 21 at 20:14
answered Mar 21 at 19:13
BazingaaBazingaa
15.3k21330
15.3k21330
thanks! the only disadvantage I can see with this approach is that the colors dont represent always the same type of data
– Laura
Mar 21 at 20:09
@Laura: I edited my solution to make the colors represent always the same items using a dictionary
– Bazingaa
Mar 21 at 20:15
Thanks this is exactly what I was looking for
– Laura
Mar 22 at 17:28
add a comment |
thanks! the only disadvantage I can see with this approach is that the colors dont represent always the same type of data
– Laura
Mar 21 at 20:09
@Laura: I edited my solution to make the colors represent always the same items using a dictionary
– Bazingaa
Mar 21 at 20:15
Thanks this is exactly what I was looking for
– Laura
Mar 22 at 17:28
thanks! the only disadvantage I can see with this approach is that the colors dont represent always the same type of data
– Laura
Mar 21 at 20:09
thanks! the only disadvantage I can see with this approach is that the colors dont represent always the same type of data
– Laura
Mar 21 at 20:09
@Laura: I edited my solution to make the colors represent always the same items using a dictionary
– Bazingaa
Mar 21 at 20:15
@Laura: I edited my solution to make the colors represent always the same items using a dictionary
– Bazingaa
Mar 21 at 20:15
Thanks this is exactly what I was looking for
– Laura
Mar 22 at 17:28
Thanks this is exactly what I was looking for
– Laura
Mar 22 at 17:28
add a comment |
A stacked bar graph can be produced in pandas via the stacked=True
option. To use this you need to make the "Date"
the index first.
import matplotlib.pyplot as plt
import pandas as pd
data1 = 'Date':['03-19-2019'],
'Total':[35],
'Solved':[19],
'Arrived':[23],
df = pd.DataFrame(data1)
df.set_index("Date").plot(kind="barh", stacked=True)
plt.show()
Comparing your figure with OP's figure in the question, it seems he/she doesn't want a stacked bar chart but all the bars to start at x=0 but zorder arranged such that the lowest bar is at the front and the highest at the back. Compare the numbers on the x-axis
– Bazingaa
Mar 22 at 10:38
Thanks @ImportanceOfBeingErnest , Bazingaa is right. I wanted all bars start as 0. I have solved now taking into account the solution provided by him/her. Thanks
– Laura
Mar 22 at 17:30
add a comment |
A stacked bar graph can be produced in pandas via the stacked=True
option. To use this you need to make the "Date"
the index first.
import matplotlib.pyplot as plt
import pandas as pd
data1 = 'Date':['03-19-2019'],
'Total':[35],
'Solved':[19],
'Arrived':[23],
df = pd.DataFrame(data1)
df.set_index("Date").plot(kind="barh", stacked=True)
plt.show()
Comparing your figure with OP's figure in the question, it seems he/she doesn't want a stacked bar chart but all the bars to start at x=0 but zorder arranged such that the lowest bar is at the front and the highest at the back. Compare the numbers on the x-axis
– Bazingaa
Mar 22 at 10:38
Thanks @ImportanceOfBeingErnest , Bazingaa is right. I wanted all bars start as 0. I have solved now taking into account the solution provided by him/her. Thanks
– Laura
Mar 22 at 17:30
add a comment |
A stacked bar graph can be produced in pandas via the stacked=True
option. To use this you need to make the "Date"
the index first.
import matplotlib.pyplot as plt
import pandas as pd
data1 = 'Date':['03-19-2019'],
'Total':[35],
'Solved':[19],
'Arrived':[23],
df = pd.DataFrame(data1)
df.set_index("Date").plot(kind="barh", stacked=True)
plt.show()
A stacked bar graph can be produced in pandas via the stacked=True
option. To use this you need to make the "Date"
the index first.
import matplotlib.pyplot as plt
import pandas as pd
data1 = 'Date':['03-19-2019'],
'Total':[35],
'Solved':[19],
'Arrived':[23],
df = pd.DataFrame(data1)
df.set_index("Date").plot(kind="barh", stacked=True)
plt.show()
answered Mar 21 at 21:56
ImportanceOfBeingErnestImportanceOfBeingErnest
140k13162241
140k13162241
Comparing your figure with OP's figure in the question, it seems he/she doesn't want a stacked bar chart but all the bars to start at x=0 but zorder arranged such that the lowest bar is at the front and the highest at the back. Compare the numbers on the x-axis
– Bazingaa
Mar 22 at 10:38
Thanks @ImportanceOfBeingErnest , Bazingaa is right. I wanted all bars start as 0. I have solved now taking into account the solution provided by him/her. Thanks
– Laura
Mar 22 at 17:30
add a comment |
Comparing your figure with OP's figure in the question, it seems he/she doesn't want a stacked bar chart but all the bars to start at x=0 but zorder arranged such that the lowest bar is at the front and the highest at the back. Compare the numbers on the x-axis
– Bazingaa
Mar 22 at 10:38
Thanks @ImportanceOfBeingErnest , Bazingaa is right. I wanted all bars start as 0. I have solved now taking into account the solution provided by him/her. Thanks
– Laura
Mar 22 at 17:30
Comparing your figure with OP's figure in the question, it seems he/she doesn't want a stacked bar chart but all the bars to start at x=0 but zorder arranged such that the lowest bar is at the front and the highest at the back. Compare the numbers on the x-axis
– Bazingaa
Mar 22 at 10:38
Comparing your figure with OP's figure in the question, it seems he/she doesn't want a stacked bar chart but all the bars to start at x=0 but zorder arranged such that the lowest bar is at the front and the highest at the back. Compare the numbers on the x-axis
– Bazingaa
Mar 22 at 10:38
Thanks @ImportanceOfBeingErnest , Bazingaa is right. I wanted all bars start as 0. I have solved now taking into account the solution provided by him/her. Thanks
– Laura
Mar 22 at 17:30
Thanks @ImportanceOfBeingErnest , Bazingaa is right. I wanted all bars start as 0. I have solved now taking into account the solution provided by him/her. Thanks
– Laura
Mar 22 at 17:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55287582%2fmultiple-data-frame-columns-plotted-in-the-same-bar-without-overlapping%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown