-2

enter image description here

I don't know how to plot the bar chart like this photo.

Can anyone help me please?

enter image description here

enter image description here

Alisha
  • 61
  • 1
  • 6
  • Try looking at this question: https://stackoverflow.com/questions/29498652/plot-bar-graph-from-pandas-dataframe – Itamar Mushkin Aug 21 '19 at 08:12
  • And this command: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.bar.html – Itamar Mushkin Aug 21 '19 at 08:13
  • Possible duplicate of [Plot bar graph from Pandas DataFrame](https://stackoverflow.com/questions/29498652/plot-bar-graph-from-pandas-dataframe) – Itamar Mushkin Aug 21 '19 at 08:13
  • @ItamarMushkin I tried those codes but X axis is not shown! can you please help me? Thanks in advance import matplotlib.pyplot as plt ax = data[['01_jan_18','01_aug_18']].plot(kind='bar', title ="V comp", figsize=(15, 10), legend=True, fontsize=12) ax.set_xlabel("Hour", fontsize=12) ax.set_ylabel("Volume", fontsize=12) plt.show() – Alisha Aug 21 '19 at 09:08
  • It's hard to read in a comment. Please Add what you tried to your question, with a minimal example (like in https://stackoverflow.com/help/how-to-ask) – Itamar Mushkin Aug 21 '19 at 09:19
  • @ItamarMushkin Thank your for your response. I've attached a new photo. – Alisha Aug 21 '19 at 09:23
  • Now I see. You want to sum by month, but your data is by hour. Please add some small sample of your data so we can make it into an example. – Itamar Mushkin Aug 21 '19 at 09:54
  • @ItamarMushkin I want to achieve the bar chart like the photo that I have provided. I meanThe photo that has colorful bar charts. – Alisha Aug 21 '19 at 10:08
  • @ItamarMushkin Can you please help? – Alisha Aug 22 '19 at 03:59

1 Answers1

0

The pandas command Series.plot.bar accepts a color keyword, where you can input a list of colors (in RGBA or color names). I'm not entirely sure on what logic does the command follow when the list is too short, but if the list is the right length, it'll work. Example:

%matplotlib inline
import pandas as pd
this_series = pd.Series([0.5, 0.6, 0.1, 0.1, 0.05, 0.05, 0.7, 0.3, 0.2], name='foo')
this_series.plot.bar(color=['blue','black', 'brown', 'red','red','green','yellow'])

EDITED:

from your comments, I think I can understand your question. In the future, please help the community help you by crafting clearer questions.

To plot the sum of values per month, with your data structure being as in the example, you can do like this:

data (see - a minimal example that is still enough to work with):

df = pd.DataFrame(columns = ['hour_formatted', '11_jan_18', '13_jan_18', '14_mar_18'],
                 data = [['00:00', 3, 3, 4], ['00:01', 6, 3, 4], ['00:01', 100, 300, 500]])

The "hour" is unneeded, and we can sum the data values per day (after all, we'll sum them by month... or so I think).

df2 = df.drop('hour_formatted', axis=1).sum()

Afterwards, we only need the month from the day. In your format, it's the second item when splitting by '_':

df2.index = [x.split('_')[1] for x in df2.index]
df2.index.rename('month', inplace=True)

Afterwards, you can group by month (see groupby from docs), and plot a bar plot:

df3 = df2.reset_index().groupby('month').sum()[0] # the [0] part is to take first column from a dataframe that's created here. There might be a more elegant way
df3.plot.bar(color=['black', 'red'])
Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32
  • Thank you for your response. My problem is to how to achieve bar chart like this in the photo not just the color of bar chart. As you can see my data has several months with several days. I want to know how to draw bar chart for each month with consideration of all of its days. – Alisha Aug 22 '19 at 07:34
  • This is the kind of information that should be in the question body. For example: "I have this data [add minimal example], I want to plot a bar chart (with colors) with the data per month". In the future, please spend more time on how to write a clear question; otherwise, it makes it hard to answer. – Itamar Mushkin Aug 22 '19 at 07:53
  • As for your question, if I understand, your data is stored in different columns (each is a day), where the rows are the time of day (hh:mm), and you want to sum by month. Am I correct? – Itamar Mushkin Aug 22 '19 at 07:54
  • the columns are days except for the first colum which is hour. and rows are density. – Alisha Aug 23 '19 at 06:08
  • Thank you so much for providing codes. All works. Could you please tell me how should I add other months as well? by the way, How can I use the real number in my data? I mean instead of these number that you have provided (['00:01', 100, 300, 500]) , how can I use the numbers that are located at the bottom of each month? – Alisha Aug 29 '19 at 05:11
  • This is an example dataframe. This code should work on your real data (if it is in the same format), with more months and with real numbers... If I understand your question correctly. – Itamar Mushkin Aug 29 '19 at 05:26
  • Thank you for your help. Can you please tell me how can I add the name of each month and its related number on the top of each chart ?? – Alisha Aug 29 '19 at 06:14
  • Thanks a lot for your help. If I want to plot data in one graph that the graph only contains each week of July, then how should I do it? Thanks in advance – Alisha Sep 01 '19 at 01:25
  • Hi @Itamar Mushkin. Can you please help me in this question? https://stackoverflow.com/questions/57751132/how-to-plot-the-graph-in-more-accurate-way Thank you – Alisha Sep 02 '19 at 03:45
  • Your question was answered there – Itamar Mushkin Sep 03 '19 at 06:41
  • The answers were not usefull. I would happy if you put comment there as well. Thank you – Alisha Sep 05 '19 at 03:53
  • Hi Itamar Mushkin, Can you pleae help me in this question? https://stackoverflow.com/questions/57888253/how-to-plot-a-graph-when-we-have-fractional-line-formula – Alisha Sep 12 '19 at 06:24