Home > Software engineering >  Plotting of data and figure text in pandas
Plotting of data and figure text in pandas

Time:12-31

I am using a data frame

df = pd.DataFrame({"Region": [1,2,3,4,5,6,7,8,9,10], "mean_w_em": [1261011,4144337.50,8457283.00,46182432.13,22421525.98,29881120.90,5111470.52,5986791.17,3262314.22, 4192573.09],"rate":[.02,.06,.16,.24,.21,.19,.02,.03,.04,.0]})

I use the following code:

ax = aa.plot(kind = 'bar', x = 'Region',
                  y = 'HPV Rate', color = 'silver',
                  linewidth = 3,figsize = (15,8))
 
ax2 = aa.plot(kind = 'line', x = 'Region',
                   y = 'Mean Weighted Emissions', secondary_y = True,
                   color='blue',marker='.',  linewidth = 0.75,
                   ax = ax)
 
#title of the plot
plt.title("HPV rate and Weighted Air Emissions by Region")
 
#labeling x and y-axis
ax.set_xlabel('Region')
ax.set_ylabel('HPV Rate')
ax2.set_ylabel('Mean Weighted Emissions')
 
#defining display layout
plt.tight_layout()
plt.figtext(0.5, 0.01, "Region 1:CT,ME,MA,NH,RI,VT Region 2:NJ,NY,PR,VI",ha="center", fontsize = 10,bbox={"facecolor":"orange", "alpha":0.6, "pad":5})
#show plot
plt.show()


I got the following plot enter image description here

My questions:

  1. How can I plot the full value for the second y-axis instead of le7?
  2. How can I avoid the overlapping of plot text with the label?
  3. I want to add the plot text explanation in the following way instead of side by side. How can I do that?

enter image description here

CodePudding user response:

You can get rid of scientific notation with get_major_formatter().set_scientific(False) and you can either put the figure text into the x label or you can use the parameter in_layout = False. I included commented out code for the x label so you can see how it could have been used there. The key to creating newlines is \n.

import pandas as pd
import matplotlib.pyplot as plt


aa = pd.DataFrame({"Region": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'Mean Weighted Emissions': [1261011, 4144337.50, 8457283.00, 46182432.13, 22421525.98,
                  29881120.90, 5111470.52, 5986791.17, 3262314.22, 4192573.09], 'HPV Rate': [.02, .06, .16, .24, .21, .19, .02, .03, .04, .0]})


ax = aa.plot(kind = 'bar', x = 'Region',
             y = 'HPV Rate', color = 'silver',
             linewidth = 3, figsize = (15, 8))

ax2 = aa.plot(kind='line', x='Region',
              y='Mean Weighted Emissions', secondary_y = True,
              color='blue', marker='.',  linewidth=0.75,
              ax=ax)

# title of the plot
plt.title("HPV rate and Weighted Air Emissions by Region")

# labeling x and y-axis
# ax.set_xlabel('Region\n\nRegion 1:CT,ME,MA,NH,RI,VT\nRegion 2:NJ,NY,PR,VI\n\n')
ax.set_xlabel('Region')
ax.set_ylabel('HPV Rate')
ax2.set_ylabel('Mean Weighted Emissions')

# change notation
ax2.yaxis.get_major_formatter().set_scientific(False)

# add text
ax.text(1, 1, "Region\n1: and your states\n2: more states", fontsize = 12)

# defining display layout
plt.tight_layout()
plt.figtext(0.5, 0.01, "Region 1:CT,ME,MA,NH,RI,VT\nRegion 2:NJ,NY,PR,VI",
            in_layout = False,
            ha="center", fontsize=10, bbox={"facecolor": "orange", "alpha": 0.6, "pad": 3})
# show plot
plt.show()

enter image description here

  • Related