Matias Ordoñez
Matias Ordoñez

Reputation: 105

How to Resolve 'alpha' Parameter Display Issue in pyplot.grid

I have the current function which generates a simple chart in matplotlib but as we see in the image the alpha parameter not seems to work, this happen in vs code, if I test this in a notebook works fine

what i need is the same format in vs code

data:

,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

code:

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)

this is the result in vs code
enter image description here

and this is the result in a notebook
enter image description here

Upvotes: 2

Views: 1869

Answers (1)

Trenton McKinney
Trenton McKinney

Reputation: 62513

  • Make sure the environment packages used by VSCode are updated, as they aren't necessarily the same as those being used by Jupyter.
    • Tested in python 3.8.11, pandas 1.3.2, matplotlib 3.4.3
    • I was originally testing with matplotlib 3.4.2, which seems to have a bug and would not set weight='bold', so if VSCode is using a different package version, there could be a bug with alpha=0.15.
  • The OP uses plt.figure(facecolor="w") and plt.figure(figsize=(15,3)), which creates two different figures (not noticeable with inline plots, but two windows will open if using interactive plots). It should be plt.figure(facecolor="w", figsize=(15, 3)).
  • The following code uses the object oriented approach with axes, which makes sure all methods are applied to the correct axes being plotted.
  • Plot the dataframe directly with pandas.DataFrame.plot, which uses matplotlib as the default backend, and returns an axes.
  • Annotations are made using 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)

enter image description here

Upvotes: 1

Related Questions