hupili / python-for-data-and-media-communication-gitbook

An open source book on Python tailed for communication students with zero background
118 stars 62 forks source link

CH09 feedbacks #77

Closed SerenaQYHuang closed 5 years ago

SerenaQYHuang commented 6 years ago
from matplotlib import pyplot as plt
data = [1, 5, 2, 3, 2]
df = pd.DataFrame(data, columns=['value'])
df
plt.bar(df.index, df.value) #pass x label value and y label value

The first line should be import pandas as pd because pyplot has already been imported before. However, pandas has not been imported.

Also, I think plt.show()should be added to make sure the figure will be displayed

SerenaQYHuang commented 6 years ago
fig, axes = plt.subplots(nrows=2, ncols=2,constrained_layout=True,figsize=(30,20)) #set a 2*2 canvas, adjust layout to more flexible, adjust figure size, axes means the location of each subplots, you can refer to the following picture below to learn more.

There is a problem with the keyword argument 'constrained_layout'

ChicoXYC commented 6 years ago

@SerenaQYHuang what's the error of question2? Its good in my testing.

SerenaQYHuang commented 6 years ago

image

I searched for this and it seems that we need the matplotlib 2.2 or above to have this function. The problem is I can't install it.

image

SerenaQYHuang commented 6 years ago

image

Do we need an account to use this function? @ChicoXYC

SerenaQYHuang commented 6 years ago
from pyecharts import Bar
#to see relationship between countries with likes and bookmarks
pd_df3 = df.groupby(['country'])['bookmark'].mean().reset_index().sort_values("bookmark",ascending=False)
attr = pd_df2.country
v1 = pd_df2.likes  #you can pass a list like data here
v2 = pd_df3.bookmark
bar = Bar("Countries by likes and bookmark")
bar.add("by likes", attr, v1, mark_line=["average"])
bar.add("by bookmark", attr, v2, mark_line=["average"])
bar

I got one merged bar chart instead of two: (see following) image

SerenaQYHuang commented 6 years ago
from bokeh.plotting import figure
from bokeh.io import show, output_file
output_file("bokeh_bar.html")
pd_df5 = df.groupby(['country'])['bookmark'].mean().reset_index().sort_values("bookmark",ascending=False)[:10]
# source = ColumnDataSource(data=pd_df5)
p = figure(x_range=pd_df5.country,plot_height=350, title="Country with bookmark",
           toolbar_location=None, tools="")
p.vbar(x=pd_df5.country, top=pd_df5.bookmark, width=0.9)
p.xgrid.grid_line_color = None
p.y_range.start = 0
show(p)

Does not work. (see following) image

ChicoXYC commented 6 years ago

image

I searched for this and it seems that we need the matplotlib 2.2 or above to have this function. The problem is I can't install it.

image

Are you install successfully?

ChicoXYC commented 6 years ago
from pyecharts import Bar
#to see relationship between countries with likes and bookmarks
pd_df3 = df.groupby(['country'])['bookmark'].mean().reset_index().sort_values("bookmark",ascending=False)
attr = pd_df2.country
v1 = pd_df2.likes  #you can pass a list like data here
v2 = pd_df3.bookmark
bar = Bar("Countries by likes and bookmark")
bar.add("by likes", attr, v1, mark_line=["average"])
bar.add("by bookmark", attr, v2, mark_line=["average"])
bar

I got one merged bar chart instead of two: (see following) image

@SerenaQYHuang this is right, you can click the legend by likes and by bookmark to see different charts.

ChicoXYC commented 6 years ago
from bokeh.plotting import figure
from bokeh.io import show, output_file
output_file("bokeh_bar.html")
pd_df5 = df.groupby(['country'])['bookmark'].mean().reset_index().sort_values("bookmark",ascending=False)[:10]
# source = ColumnDataSource(data=pd_df5)
p = figure(x_range=pd_df5.country,plot_height=350, title="Country with bookmark",
           toolbar_location=None, tools="")
p.vbar(x=pd_df5.country, top=pd_df5.bookmark, width=0.9)
p.xgrid.grid_line_color = None
p.y_range.start = 0
show(p)

Does not work. (see following) image

@SerenaQYHuang I check out again, it works in my environment. Maybe we can have a meeting, I will check where it went wrong.

ChicoXYC commented 6 years ago

image

Do we need an account to use this function? @ChicoXYC

@SerenaQYHuang This is because I use the plot online method, which requires an account. I just updated the plot.offline method, you can try without creating an account. https://github.com/hupili/python-for-data-and-media-communication-gitbook/blob/master/notes-week-09.md#plotly

ChicoXYC commented 6 years ago

@SerenaQYHuang have the problems solved?

SerenaQYHuang commented 6 years ago

All the problems are solved except for the last one

ChicoXYC commented 5 years ago

Question is not clear, closed for now and will be reopened if necessary.