2

I would like to print the DataFrame besides the plot. What would be a pythonic way to do that?

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'Age':[21,22,23,24,25,26,27,28,29,30],'Count':[4,1,3,7,2,3,5,1,1,5]})
print(df)
   Age  Count
0   21      4
1   22      1
2   23      3
3   24      7
4   25      2
5   26      3
6   27      5
7   28      1
8   29      1
9   30      5

plt.rcParams['figure.figsize']=(10,6)
fig,ax = plt.subplots()
font_used={'fontname':'pristina', 'color':'Black'}
ax.set_ylabel('Count',fontsize=20,**font_used)
ax.set_xlabel('Age',fontsize=20,**font_used)
plt.plot(df['Age'],df['Count'])

I would like to have a Graph like this. How can I have the DataFrame's plotted values are printed alongside?: enter image description here

2
  • Alongside or as part of the plot? Commented Jan 19, 2021 at 15:58
  • 1
    I mean as part of plot. All I want to be able to see the values plotted. Commented Jan 19, 2021 at 16:00

3 Answers 3

2

You can use ax.text to add the DataFrame to the plot. DataFrames have a .to_string method which makes formatting nice. Supply index=False to remove the row index.

plt.rcParams['figure.figsize']=(10, 6)
fig,ax = plt.subplots()
font_used={'fontname':'pristina', 'color':'Black'}
ax.set_ylabel('Count',fontsize=20,**font_used)
ax.set_xlabel('Age',fontsize=20,**font_used)

# Adjust to where you want. 
ax.text(x=28.5, y=4.5, s=df.to_string(index=False))

plt.plot(df['Age'],df['Count'])
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

This looks fine. Very close to my expectation. Would there be a way to dynamically place the values, instead of hard coding it (x=28.5,y=4.5) ??
@cph_sto hmmm, there should be a way, I'm not super great with how that stuff scales. You could make it based off of the span of Age and the span of Count, but it still might collide with stuff. I think it gets really complicated to dynamically place text to ensure no collisions, but some matplotlib people would know a lot more. Or if you want outside of the plot you could always place it outside like x, maybe like: ax.text(x=df['Age'].max()*1.02, y=df.Count.max()*0.6, s=df.to_string(index=False))
I remember seeing something where the text automatically chooses to places itself dynamically .....
1

Another option is to use the function plt.table():

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({'Age':[21,22,23,24,25,26,27,28,29,30],'Count':[4,1,3,7,2,3,5,1,1,5]})
plt.rcParams['figure.figsize']=(10,15)
fig,ax = plt.subplots()
plt.subplots_adjust(left=0.1, right=0.85, top=0.9, bottom=0.1)
font_used={'fontname':'pristina', 'color':'Black'}
ax.set_ylabel('Count',fontsize=20,**font_used)
ax.set_xlabel('Age',fontsize=20,**font_used)
plt.plot(df['Age'],df['Count'])
ax.table(cellText=df['Count'].map(str),
                  rowLabels=df['Age'].map(str),
                    colWidths=[0.2,0.25],
                  loc='right')
plt.show()

This approach will create a table with their respective lines. Just make sure to adjust the plot with subplots_adjust() afterwards.

enter image description here

1 Comment

Is there a way to push the table out from the plot, as the first column in entrenched inside plot and can we increase the font of the table?
0

Pandas has a to_html function you can use and place the html next to it. What are you placing the graph and Dataframe into?

df.to_html()

1 Comment

I actually followed this approach sometime back, but it makes problem a bit more cumbersome .....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.