How do I visualize the below dataframe in Python. I wish to visualize the data in a bar chart where the Year_of_Release is the X axis, Global_Sales is the bar height & the genre is the legend. The bar has to be colored separately for each Genre. I have shared a sample of what I'm looking for. The sample graph was created on R using GGPLOT.
Below are the column definitions
Year_of_Release - Year of Release
Genre - Game Genre
Global_Sales - Revenue made by a Genre in that given year
Images of the data frame & desired Bar plot as as below
Data Frame:
Desired Bar Chart:
you can use the code below to plot that you need...
fig, ax = plt.subplots(figsize=(12,6))
sns.set_theme(style="darkgrid")
ax=sns.barplot(x="Year_of_Release", y="Global_Sales", hue="Genre", dodge=False, palette="rocket", data=df)
plt.xticks(rotation=90)
ax.grid(True)
ax.legend(loc='upper left')
plt.show()
Plot
with dummy data
...
Related
I need to make the following chart: Number of Companies, Donations vs Year as a bar chart.
The following is my data:
Year = [2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018]
No_Companies = [123558,132335,147606,155790,161211,169784,174599,183888,198727,207317,217357,228996]
Donations=[144932,304607,642328,870509,1205382,1094624,2089240,2325322,2387036,3096069,4204255,3500766]
From what I have seen from other questions, most seem to have either their data in a dataframe or a list like [[x1,y1],[x2,y2]].
How can I get the chart I need from the data I have?
You can check this link out: Plot bar and line in same plot, different y-axes using matplotlib (no pandas)
The implementation can be done as follows:
plt.figure(1, figsize=(10,10))
barchart = plt.bar(Year, No_Companies, color='red')
plt.ylabel('No Companies')
plt.twinx()
barchart1 = plt.bar(Year, Donations, color='blue')
plt.ylabel('Donations')
Graph
I have three types of data that are equivalent to the monthly total, I need to create a stacked area chart that has a layer of a column chart on top showing the same value per month. I managed to make the two graphs with matplot but it shows me them separately.
data1=[13,17,19,20,23,21,27,25]
data2=[7,6,8,9,8,9,9,9]
data3=[53,45,53,61,59,67,69,68]
mes=["Enero","Febrero","Marzo","Abril","Mayo", "Junio", "Julio", "Agosto"]
# Basic stacked area chart.
pal = ["#b5030f", "#efb810", "#0a497b"]
plt.figure(figsize=(9,7))
plt.stackplot(mes,data3, data2, data1, colors=pal)
plt.legend(loc='upper left')
# Bar plot
plt.figure(figsize=(9,7))
plt.bar(mes,data3,color="#5AAB61")
plt.bar(mes,data2,color="#5AAB61",bottom=np.array(data3))
plt.bar(mes,data1,color="#5AAB61",bottom=np.array(data3)+np.array(data2))
plt.legend(loc="lower left",bbox_to_anchor=(0.8,1.0))
plt.show()
Hi to all the experts,
I'm new to Python and Data Science and actually I'm learning with a real world example to get into Data Science.
I loaded already a CSV and did some work on the data. That's ok. I have the following dataframe:
dataframe
Then, I created a Pivot from the dataframe:
pivot = pd.pivot_table(
data=df,
index=['Category', 'month', 'year'],
values='Amount',
aggfunc='sum',
margins=True)
Now, I have the following dataframe:
new dataframe
Now, I want to plot the following (line chart or bar chart):
X: Month
Y: Amount
But, I want that for explicit Category like "Business" to see, how the amount changed over the periods.
Whats the best way, to plot a clear, beautiful chart with matplotlib?
Thanks in Advance.
Many Greetings
Leon
You can use the below code to build the graphs. I think the stacked bar graphs would be a good way to see the Amount in each month.
Code
## Add AFTER you have created your pivot table
dfg = pivot.reset_index().set_index(['Month', 'Category']).sort_index(level=[0,1])
fig, ax = plt.subplots(figsize=(6,4))
dfg['Amount'].unstack().plot.bar(stacked=True, ax=ax, legend = False)
ax.set_xticklabels(sorted(df.Month.unique()), rotation=0)
ax.set_title('My Graph')
fig.legend(loc="upper right", bbox_to_anchor=(1.1, 0.9))
plt.show()
Stacked Bar graph
Unstacked Bar graph
Change stacked = True to stacked = False to see the bars next to each other, if you are not a fan of stacked bars
Line Graphs
You can also use line graphs, but not my personal preference.
Replace the plot.bar line in above code to
dfg['Amount'].unstack().plot(kind='line', marker='o', ax=ax, legend = False)
I have 3 columns in my data frame - Year_MONTH, POP and MAX_RATE
When ploting, POPT-the dataframe with year_month on x- axis and max_rate as an line chart, POP as secondary axis with area chart.
I wrote this.
ax=POPT['MAX_RATE'].plot(kind='line', color="blue")
POPT[['POP']].plot(kind='area',color="orange", secondary_y=True, ax=ax)
here basically below line(MAX_RATE) is not seen
while I do other way round
ax = POPT[['POP']].plot(kind='area',secondary_y=True,color="orange")
POPT['MAX_RATE'].plot(kind='line', color='blue', xlim=ax.get_xlim())
this generates like this
here i cant see the line as it is not in the frame or primary y axis is not showing properly.
when I wrote like this
POPT['MAX_RATE'].plot(kind='line', color="blue")
POPT[['POP']].plot(kind='area', secondary_y=True)
this just split as two seperate charts
how do I correct this?
I have a data frame with a related salary to the major.
I am trying to create horizontal bar charts of the majors sorted by salary.
My code looks like this:
fig, ax = plt.subplots()
topTenMajor = df[['Major','Salary']].sort_values('Salary', ascending=False).set_index('Major')
topTenMajor.sort_values('Salary', ascending=True).plot.barh(figsize=(5,10))
ax.set_title('Majors by Salary')
ax.set_xlabel('Salary')
ax.set_ylabel('Majors')
However, my chart shows one emptly plots on top with title, x label and y label,
and then a horizontal barchart under the empty plots without title and labels.
Why is this happening?
Thanks for any help!
barh will plot in a new figure / axes by default.
Either you need to tell it to plot in the fig, ax you created before.
Or you can set title and labels in the active figure automatically created:
topTenMajor = df[['Major','Salary']].sort_values('Salary', ascending=False).set_index('Major')
topTenMajor.sort_values('Salary', ascending=True).plot.barh(figsize=(5,10))
plt.title('Majors by Salary')
plt.xlabel('Salary')
plt.ylabel('Majors')