简体   繁体   中英

Reformatting y axis values in a multi-line plot in Python

Updated with more info

I've seen this answered on here for single line plots, but I need help with a plot showing two variables, if that matters at all... I am fairly new to python in general. My line graph shows two different departments' funding over the years. I just want to reformat the y axis to display as a number in the hundreds of millions.

Using a csv for the general public funding report of Minneapolis.

msp_df = pd.read_csv('Minneapolis_Data_Snapshot_v2.csv',error_bad_lines=False)
msp_df.info()

Saved just the two depts I was interested in, to a dataframe.

CPED_df = (msp_df['Unnamed: 0'] == 'CPED')
msp_df.iloc[CPED_df.values]

police_df = (msp_df['Unnamed: 0'] == 'Police')
msp_df.iloc[police_df.values]

("test" is the new name of my data frame containing all the info as seen below.)

 test = pd.DataFrame({'Year': range(2014,2021), 
                      'CPED': msp_df.iloc[CPED_df.values].T.reset_index(drop=True).drop(0,0)[5].tolist(), 
                      'Police': msp_df.iloc[police_df.values].T.reset_index(drop=True).drop(0,0)[4].tolist()})

The numbers from the original dataset were being read as strings because of the commas so had to fix that first.)

test['Police2'] = test['Police'].str.replace(',','').astype(int)
test['CPED2'] = test['CPED'].str.replace(',','').astype(int)

And here is my code for the plot. It executes, I'm just wanting to reformat the y axis number scale. Right now it just shows up as a decimal. (I've already imported pandas and seaborn and matploblib)

plt.plot(test.Year, test.Police2, test.Year, test.CPED2)
plt.ylabel('Budget in Hundreds of Millions')
plt.xlabel('Year')

Current plot

Any help super appreciated: Thanks :)

the easiest way to reformat the y axis, to force it to take certain values is to use

 plt.yticks(ticks, labels)

for example if you want to have only display values from 0 to 1 you can do:

plt.yticks([0,0.2,0.5,0.7,1], ['a', 'b', 'c', 'd', 'e'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM