romain jouin romain jouin - 3 months ago 8
Python Question

Pandas transforming hbar in line

I have a pandas hbar plot, but I would like to have a line instead of the bars (just a line going to the top of the bars).

Is that possible?

I have

b3["R2_foret"].plot(legend=False, kind='barh')


which gives me

enter image description here

I would like not to see all this blue, but just a line going from one value to the next one.
The issue is to make a vertical line.

EDIT



Following the here below solution by unutbu, with alpha=0

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

b3 = pd.DataFrame({'R2_foret': [0.79381443298969068, 0.7766323024054983, 0.7903780068728522, 0.78006872852233677, 0.79725085910652915, 0.79725085910652915, 0.8041237113402061, 0.7903780068728522, 0.7903780068728522, 0.81443298969072153, 0.80068728522336763, 0.80756013745704458, 0.85567010309278368, 0.8556701030927838, 0.87628865979381465, 0.84536082474226804, 0.86597938144329922, 0.87628865979381454, 0.85910652920962216, 0.85910652920962227, 0.87579774177712344, 0.87628865979381465, 0.84536082474226792, 0.86597938144329922, 0.87628865979381454, 0.88316151202749149, 0.89347079037800703, 0.90378006872852246, 0.90378006872852246, 0.90034364261168398, 0.9106529209621993, 0.90378006872852246, 0.90721649484536093, 0.9101620029455082, 0.9106529209621993, 0.92096219931271495, 0.90721649484536093, 0.92096219931271495, 0.92096219931271495, 0.91408934707903777]})

b3['y'] = np.arange(len(b3))
ax = b3.plot(x='R2_foret', y='y', style=['-'])
b3['R2_foret'].plot(kind='barh', ax=ax, alpha=0)
plt.xlim(0,1)
plt.tick_params(left='off', labelleft='off')
plt.show()


enter image description here

Answer

Assign a new column of y values:

b3['x'] = np.arange(len(b3))

Then plot y versus R2_foret:

 b3.plot(x='R2_foret', y='y', style=['o-'])

For example,

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

b3 = pd.DataFrame({'R2_foret': [0.79381443298969068, 0.7766323024054983, 0.7903780068728522, 0.78006872852233677, 0.79725085910652915, 0.79725085910652915, 0.8041237113402061, 0.7903780068728522, 0.7903780068728522, 0.81443298969072153, 0.80068728522336763, 0.80756013745704458, 0.85567010309278368, 0.8556701030927838, 0.87628865979381465, 0.84536082474226804, 0.86597938144329922, 0.87628865979381454, 0.85910652920962216, 0.85910652920962227, 0.87579774177712344, 0.87628865979381465, 0.84536082474226792, 0.86597938144329922, 0.87628865979381454, 0.88316151202749149, 0.89347079037800703, 0.90378006872852246, 0.90378006872852246, 0.90034364261168398, 0.9106529209621993, 0.90378006872852246, 0.90721649484536093, 0.9101620029455082, 0.9106529209621993, 0.92096219931271495, 0.90721649484536093, 0.92096219931271495, 0.92096219931271495, 0.91408934707903777]}) 

b3['y'] = np.arange(len(b3))
ax = b3.plot(x='R2_foret', y='y', style=['o-'])
b3['R2_foret'].plot(kind='barh', ax=ax, alpha=0.3)
plt.xlim(0,1)
plt.tick_params(left='off', labelleft='off')
plt.show()

enter image description here

Comments