I am reading a csv and plotting in Python using the following code:
import pandas as pd
from matplotlib import pyplot as plt
data = pd.read_csv('Direccion del archivo Csv', header = 0, sep=';')
g = data.groupby(["SUBDETERMINANTE"])["POSITIVAS", "POSIBLES"].sum()
1) How to order g groupby from highest to lowest based on the positive field so that when making the graph they are in that order?
g.plot(kind='barh', legend='Reverse')
plt.title(u'ANALISIS DE SUBDETERMINANTES', size = 12.0)
plt.xlabel(u'Suma', size = 12.0)
plt.ylabel('SubDeterminantes', size = 12.0)
fig_size = plt.rcParams["figure.figsize"]
print ("Current size:", fig_size)
fig_size[0] = 10.0
fig_size[1] = 15.0
plt.savefig('detalle')
Show the graph with the data from highest to lowest according to the total number of positives:
2) At the end of the graph I download it in PNG, how can I make it show the entire name?
SOLVED THANKS TO ABULAFIA'S RESPONSE
import pandas as pd
from matplotlib import pyplot as plt
data = pd.read_csv('Direccion donde esta el archivo csv', header = 0, sep=';')
g = data.groupby(["SUBDETERMINANTE"])["POSITIVAS",
"POSIBLES"].sum().sort_values(by="POSITIVAS")
g.plot(kind='barh', legend='Reverse')
plt.title(u'ANALISIS DE SUBDETERMINANTES', size = 12.0)
plt.xlabel(u'Suma', size = 12.0)
plt.ylabel('SubDeterminantes', size = 12.0)
fig_size = plt.rcParams["figure.figsize"]
print ("Current size:", fig_size)
fig_size[0] = 10.0
fig_size[1] = 15.0
plt.savefig('detalle', bbox_inches='tight')
As I do not have your data, I cannot verify if my answer is correct. In any case, I think it would be as follows:
1. Sort the dataframe
Dataframes have the
sort_values()
. So try the following:(the parentheses that I surrounded everything with are a trick to be able to break the expression into several lines, for better readability, you can put it all on one line and without the parentheses, if you prefer)
2. Image export
It is not clear to me how you export it, whether through
savefig()
or through some option of your graphical user interface. If it is withsavefig()
often problems of this style are solved by passing the parameterbbox_inches='tight'