7

I have the following:

indyes = tuple(yesSeries.index)
indno = tuple(nodSeries.index)
width = 3

p1 = plt.bar(indyes, yesSeries, label="Example one", color='SkyBlue')
p2 = plt.bar(indno, nodSeries, label="Example two", color='IndianRed')
plt.legend()
plt.xlabel('bar number')
plt.ylabel('bar height')

plt.title('Epic Graph\nAnother Line! Whoa')

plt.show()

It plots my graph as a stacked bar graph: enter image description here

When I try to add + width to the second bar graph, indno + width so they are not stacked, rather side by side I get the following error: TypeError: can only concatenate tuple (not "int") to tuple.

I have set my Pandas Series into a tuple to extract the date and count information.

How can I get two parallel vertical bar plots?

7
  • What's the reason to use a tuple here? Do you want to add 3 days to each item in the index? Commented Nov 9, 2018 at 16:01
  • @ ImportanceOfBeingErnest I have my dataframe grouped by a timestamp, and my final dataset is a pandas.core.series.Series with a timestamp and counts. I'm using the tuple to access the timestamp and counts in the series for my x and y vales in my plot. The plot seems to be working fine, I prefer 2 bars rather than a stacked bar. Commented Nov 9, 2018 at 16:08
  • Not sure if that answers my question. Anyways, you might also decide to provide a minimal reproducible example here, such that people can simply provide an answer without understanding the motivation. Commented Nov 9, 2018 at 16:11
  • @ ImportanceOfBeingErnest No, I do not want to add 3 days. I want to have 2 horizontal bar graphs, and not stacked bar graphs. Commented Nov 9, 2018 at 16:27
  • Horizontal bar graphs are created via barh. Commented Nov 9, 2018 at 16:29

1 Answer 1

18

I believe that what you are looking for can be done creating a DataFrame from your Series and then using the plot.bar function.

In below code I generate data and create the graph as you may want to.

import matplotlib.pyplot as plt
import pandas as pd
times = pd.date_range('2018-09-01', periods=7, freq='5D')
yesSeries = pd.Series([1800,2000,3000,1000,2000,1500,1700], index=times)
nodSeries = pd.Series([200,500,700,600,300,50,0], index=times)

df = pd.DataFrame({"Example one":yesSeries,"Example two":nodSeries})
ax = df.plot.bar(color=["SkyBlue","IndianRed"], rot=0, title="Epic Graph\nAnother Line! Whoa")
ax.set_xlabel("date")
ax.set_ylabel("counts")
ax.xaxis.set_major_formatter(plt.FixedFormatter(times.strftime("%b %d %Y")))
plt.show()

This results in below graph.

Bar plot

If it seems that the dates appear to be cluttered you can insert below line before the plt.show() code:

plt.gcf().autofmt_xdate()

Which would result in below graph.

Bar plot with dates with autofmt

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.