I have a MySQL query that produces the below table:
Year | Month | Quarter | Sales |
---|---|---|---|
1992 | January | 1 | 701.0 |
1992 | February | 1 | 658.0 |
1992 | March | 1 | 731.0 |
1992 | April | 2 | 816.0 |
1992 | May | 2 | 856.0 |
1992 | June | 2 | 853.0 |
the table continues till 2021. Now I want to create a bar plot in python that groups the year and the quarters on the X-axis. In other words, imagine a graph with the X-axis has a year within which 4 bars(for each Quarter) are shown and this continues for all the years. I can't figure out how to get this plot code-wise. I can manually enter the sales values and constructed a list within the list but it won't be effective to do it for 30 years of data. Any suggestions?
CodePudding user response:
If you need a barplot divided in quarter with some value you can transform your query into a dataframe like in that code
import pandas as pd
years = [1999, 2000]
q1 = [10, 20]
q2 = [20, 10]
q3 = [30, 40]
df = pd.DataFrame({'q1': q1,
'q2': q2,
'q3': q3}, index=years)
df.plot.bar(rot=0)
To obtain a plot like that:
Or if you prefer a stacked plot you can use this plot line
df.plot.bar(stacked=True)
To obtain a plot like that: