Certificate/data science-IBM

area plot, histogram, bar chart, annotate

Olivia-BlackCherry 2023. 5. 9. 17:51

area plot

df.top5.plot(kind='area')

df_top5.index = df_top5.index.map(int)
df_top5.plot(kind='area',
             stacked=True,
             figsize=(20, 10))  # pass a tuple (x, y) size

plt.title('Immigration Trend of Top 5 Countries')
plt.ylabel('Number of Immigrants')
plt.xlabel('Years')

plt.show()

 

 

histogram

### type your answer here
df_cof =df_can.loc[['Greece', 'Albania', 'Bulgaria'], years]
df_cof=df_cof.transpose()
count, bin_edges=np.histogram(df_cof, 15)
df_cof.plot(kind='hist', figsize=(10, 6), bins=15, alpha=0.35, xticks=bin_edges, color=['coral', 'darkslateblue', 'mediumseagreen'])
plt.show()

 

all(isinstance(column, str) for column in df_can.columns)

IS INSTANCE COLUMN-> STRING인지 확인

df_can.columns = list(map(str, df_can.columns))

모든 칼럼의 타입을 STRING으로 변경 

 

 

bar chart

df_iceland.plot(kind='bar', figsize=(10, 6))

plt.xlabel('Year') # add to x-label to the plot
plt.ylabel('Number of immigrants') # add y-label to the plot
plt.title('Icelandic immigrants to Canada from 1980 to 2013') # add title to the plot

plt.show()

annotate 추가

plt.annotate('yahoo',  # s: str. Will leave it blank for no text
             xy=(32, 60),  # place head of the arrow at point (year 2012 , pop 70)
             xytext=(10, 20),  # place base of the arrow at point (year 2008 , pop 20)
             xycoords='data',  # will use the coordinate system of the object being annotated
             arrowprops=dict(arrowstyle='->', connectionstyle='arc3', color='blue', lw=2)
             )

  • s: str, the text of annotation.
  • xy: Tuple specifying the (x,y) point to annotate (in this case, end point of arrow).
  • xytext: Tuple specifying the (x,y) point to place the text (in this case, start point of arrow).
  • xycoords: The coordinate system that xy is given in - 'data' uses the coordinate system of the object being annotated (default).
  • arrowprops: Takes a dictionary of properties to draw the arrow:
    • arrowstyle: Specifies the arrow style, '->' is standard arrow.
    • connectionstyle: Specifies the connection type. arc3 is a straight line.
    • color: Specifies color of arrow.
    • lw: Specifies the line width.