【问题标题】:plt grid ALPHA parameter not working in matplotlibplt grid ALPHA 参数在 matplotlib 中不起作用
【发布时间】:2021-09-10 08:32:00
【问题描述】:

我有一个在 matplotlib 中生成简单图表的当前函数,但正如我们在图像中看到的,alpha 参数似乎不起作用,这发生在 vs 代码中,如果我在笔记本中测试它可以正常工作

我需要的是vs代码中相同的格式

数据:

,hora,id
0,0,10
1,1,3
2,2,2
3,3,3
4,4,5
5,5,3
6,6,11
7,7,32
8,8,41
9,9,71
10,10,75
11,11,70
12,12,57
13,13,69
14,14,50
15,15,73
16,16,47
17,17,64
18,18,73
19,19,54
20,20,45
21,21,43
22,22,34
23,23,27

代码:

import pandas as pd
from matplotlib import pyplot as plt

dfhoras=pd.read_clipboard(sep=',')

def questionsHour(dfhoras):        
        x = dfhoras['hora']
        y = dfhoras['id']
        horas=[x for x in range(24)]
        plt.figure(facecolor="w")
        plt.figure(figsize=(15,3))
        plt.rcParams['axes.spines.top'] = False
        plt.bar(x, y,linewidth=3,color="#172a3d")
        plt.xticks(horas,fontweight='bold',color="#e33e31",fontsize=9)
        plt.yticks(fontweight='bold',color="#e33e31",fontsize=9)
        plt.grid(color="#172a3d", linestyle='--',linewidth=1,axis='y',alpha=0.15)
        #aca creo las etiquetas de los puntos
        for x,y in zip(x,y):
                label = "{:.2f}".format(y)
                plt.annotate(label,
                                (x,y),
                                textcoords="offset points",
                                xytext=(0,5),
                                ha='center',
                                fontsize=9,
                                fontweight='bold',
                                color="#e33e31")
        plt.savefig('questions1.png',dpi=600,transparent=True,bbox_inches='tight')
        
questionsHour(dfhoras)

这是vs代码中的结果

这是笔记本中的结果

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:
    • 确保 VSCode 使用的环境包已更新,因为它们不一定与 Jupyter 使用的环境包相同。
      • python 3.8.11pandas 1.3.2matplotlib 3.4.3 中测试
      • 我最初是用matplotlib 3.4.2 进行测试的,它似乎有一个错误并且不会设置weight='bold',所以如果VSCode 使用不同的包版本,alpha=0.15 可能会有一个错误。
    • OP 使用plt.figure(facecolor="w")plt.figure(figsize=(15,3)),它们创建了两个不同的图形(内联图不明显,但如果使用交互式绘图,则会打开两个窗口)。应该是plt.figure(facecolor="w", figsize=(15, 3))
    • 以下代码使用axes 的面向对象方法,可确保将所有方法应用于正确的axes 被绘制。
    • 直接用pandas.DataFrame.plot绘制数据帧,它使用matplotlib作为默认后端,并返回一个axes
    • 使用matplotlib.pyplot.bar_label 进行注释
    import pandas as pd
    
    # test data
    data = {'hora': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23], 'id': [10, 3, 2, 3, 5, 3, 11, 32, 41, 71, 75, 70, 57, 69, 50, 73, 47, 64, 73, 54, 45, 43, 34, 27]}
    df = pd.DataFrame(data)
    
    # plot function
    def questionsHour(df):        
    
        ax = df.plot(x='hora', y='id', figsize=(15, 3), linewidth=3, color="#172a3d", rot=0, legend=False, xlabel='', kind='bar', width=0.75)
        
        ax.set_xticklabels(ax.get_xticklabels(), weight='bold', color="#e33e31", fontsize=9)
        ax.set_yticks(ax.get_yticks())  # prevents warning for next line
        ax.set_yticklabels(ax.get_yticks(), weight='bold', color="#e33e31", fontsize=9)
        
        ax.grid(color="#172a3d", linestyle='--', linewidth=1, axis='y', alpha=0.15)
        
        ax.spines['top'].set_visible(False)
        ax.bar_label(ax.containers[0], fmt='%.2f', fontsize=9, weight='bold', color="#e33e31")
    
           
    questionsHour(df)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 2018-02-06
      • 2021-02-09
      • 2011-09-24
      • 1970-01-01
      相关资源
      最近更新 更多