简体   繁体   中英

Python - Add Y-Axis Values To Plot

I would like to add y-axis values to my plot but am unsure how to.

The plot is displayed below:

在此处输入图像描述

The code used to generate the plot is given below:

pd.options.display.float_format = "{:,.0f}".format

DATE_PERIOD = WORLDWIDE_COVID19_DATASET_TOTAL["DATE"]
MONTHLY_NUMBER_OF_CASES = WORLDWIDE_COVID19_DATASET_TOTAL["TOTAL NUMBER OF COVID-19 CASES"]

fig = plt.figure(figsize = (48, 20))
plt.plot(DATE_PERIOD, MONTHLY_NUMBER_OF_CASES, color = "#4b0082")
plt.title("TRENDLINE OF THE TOTAL NUMBER OF COVID-19 CASES WORLDWIDE FROM JANUARY 2020 TO JULY 2020", fontsize = 24)
plt.xlabel("DATE PERIOD (JANUARY 2020 TO JULY 2020)", fontsize = 20)
plt.ylabel("TOTAL NUMBER OF COVID-19 CASES WORLDWIDE", fontsize = 20)
plt.xticks(fontsize = 18)
plt.yticks(fontsize = 18)
ax = plt.gca()
ax.yaxis.set_major_formatter(mticker.ScalarFormatter())
ax.yaxis.get_major_formatter().set_scientific(False)
ax.yaxis.get_major_formatter().set_useOffset(False)
plt.show()

As such, how can I add y-axis only values?
Also, can I edit the y-axis only value' font size, type and boldness?

Many thanks!

what do you mean by how can I add y-axis only values?

you can add more values to panda dataframe column MONTHLY_NUMBER_OF_CASES and yes you can edit the y-axis only value' font size, type and boldness.

If you're using seaborn then direct use the feature sns.set(font_scale=3) .

import seaborn as sns
import matplotlib.pyplot as plt

flights = sns.load_dataset("flights")
flights.head()

may_flights = flights.query("month == 'May'")

sns.set(font_scale=1.5)  # crazy big this will change font for all the figure

# or use this
g = sns.lineplot(data=may_flights, x="year", y="passengers", lw=3)
g.axes.set_title("Title",fontsize=50)
g.set_xlabel("X Label",fontsize=30)
g.set_ylabel("Y Label",fontsize=20)
g.tick_params(labelsize=20)

在此处输入图像描述

Thanks for the help and advice. I believe I was not explicit enough in presenting my query.

However, I managed to find an answer on own.

The plot is supposed to look like this:

在此处输入图像描述

I obtained this plot from a "for loop".
The "for loop" is required to generate the annotations for the y-axis values on the plot.

The code is given as:

# Displaying the trendline of the total number of COVID-19 cases worldwide from January 2020 to July 2020.

# Displaying all numbers to 0 decimal places
pd.options.display.float_format = "{:,.0f}".format

DATE_PERIOD = WORLDWIDE_COVID19_DATASET_TOTAL["DATE"]
MONTHLY_NUMBER_OF_CASES = WORLDWIDE_COVID19_DATASET_TOTAL["TOTAL NUMBER OF COVID-19 CASES"]

fig = plt.figure(figsize = (48, 20))
plt.plot(DATE_PERIOD, MONTHLY_NUMBER_OF_CASES, color = "#4b0082")
plt.title("TRENDLINE OF THE TOTAL NUMBER OF COVID-19 CASES WORLDWIDE FROM JANUARY 2020 TO JULY 2020", fontsize = 24)

for x, y in zip(DATE_PERIOD, MONTHLY_NUMBER_OF_CASES):

    label = "{:.0f}".format(y)

    plt.annotate(label, # this is the text
                 (x, y), # this is the point to label
                 fontsize = 22, # this is the label font size
                 textcoords = "offset points", # how to position the text
                 xytext = (0, 10), # distance from text to points (x,y)
                 ha = "right") # horizontal alignment can be left, right or center

plt.xlabel("DATE PERIOD (JANUARY 2020 TO JULY 2020)", fontsize = 20)
plt.ylabel("TOTAL NUMBER OF COVID-19 CASES WORLDWIDE", fontsize = 20)
plt.xticks(fontsize = 18)
plt.yticks(fontsize = 18)
ax = plt.gca()
ax.yaxis.set_major_formatter(mticker.ScalarFormatter())
ax.yaxis.get_major_formatter().set_scientific(False)
ax.yaxis.get_major_formatter().set_useOffset(False)
plt.show()

Cheers ~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM