【问题标题】:Python - Add Y-Axis Values To PlotPython - 添加 Y 轴值以绘图
【发布时间】:2021-01-30 16:01:09
【问题描述】:

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

情节如下图所示:

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

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 轴唯一值的字体大小、类型和粗体吗?

非常感谢!

【问题讨论】:

  • 如何添加仅 y 轴的值是什么意思?是的,您可以编辑仅 y 轴值的字体大小、类型和粗体。
  • but am unsure how to - 你有没有花时间在Matplotlib documentation/Tutorials上?或者通过Matplotlib Gallery搜索?欢迎来到 SO。这不是讨论论坛或教程。请使用tour 并花时间阅读How to Ask 以及该页面上的其他链接。

标签: python matplotlib seaborn


【解决方案1】:

我怎样才能只添加 y 轴值?

您可以向 panda 数据框列 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)

【讨论】:

    【解决方案2】:

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

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

    情节应该是这样的:

    我从“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()
    

    干杯~

    【讨论】:

      猜你喜欢
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      相关资源
      最近更新 更多