Python - 将 Y 轴值添加到绘图

时间:2021-01-30 16:01:09

标签: python matplotlib seaborn

我想将 y 轴值添加到我的绘图中,但不确定如何添加。

图如下所示:

enter image description here

用于生成绘图的代码如下:

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()

因此,如何仅添加 y 轴值?
另外,我可以编辑仅 y 轴值的字体大小、类型和粗体吗?

非常感谢!

2 个答案:

答案 0 :(得分:1)

我如何仅添加 y 轴值是什么意思?

您可以向熊猫数据框列 MONTHLY_NUMBER_OF_CASES 添加更多值,是的,您可以编辑仅 y 轴值的字体大小、类型和粗体。

如果您使用的是 seaborn,则直接使用功能 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)

enter image description here

答案 1 :(得分:0)

感谢您的帮助和建议!我认为我在提出查询时不够明确。

但是,我设法自己找到了答案。

情节应该是这样的:

enter image description here

我从“for 循环”中获得了这个图。
需要“for循环”来为绘图上的y轴值生成注释。

代码如下:

# 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()

干杯~

相关问题