grids of graphs in matplotlib - python

Using the AXIS notation for matplotlib has allowed me to manually plot a grid of 2x2 or 3x3 or whatever size grid (if I know what size grid I want beforehand.)
However, how do you determine what size grid is needed automatically. Like what if you don't know how many unique values are in a column that you want to graph?
I am thinking there must be a way of doing this in a loop and figuring out based on the number of unique values in the column this is how big the graph needs to be.
Example
When I plot this for some reason it doesn't show month_name on the x axis (as in Jan, Feb, Marc etc)
avg_all_account.plot(legend=False,subplots=True,x='month_date',figsize=(10,20))
plt.xlabel('month')
plt.ylabel('number of proposals')
Yet when I plot subplots on a figure and specify x axis paremeter x='month_name' The month name appears on the plot here:
f = plt.figure()
f.set_figheight(8)
f.set_figwidth(8)
f.sharex=True
f.sharey=True
#graph1 = f.add_subplot(2,2,1)
avg_all_account.ix[0:,['month_date','number_open_proposals_all']].plot(ax=f.add_subplot(331),legend=False,subplots=True,x='month_date',y='number_open_proposals_all',title='open proposals')
plt.xlabel('month')
plt.ylabel('number of proposals')
Thus because the subplot method worked and showed the month_name on the x axis, and my x and y axis labels: I wanted to know how would I work out how many subplots I would need without first calculating it, then writing out each line and hard coding the subplot position?

Related

How to change tick labels for plot chart from 19:00-7:00 hours in matplotlib

I am trying to plot line charts for both nighttime and daytime to compare the differences in traffic volume in both time periods.
plt.subplot(2,1,1) #plot in grid chart to better compare differences
by_hour_business_night['traffic_volume'].plot.line()
plt.title('Business Nights Traffic Volume by Hours')
plt.ylabel('Traffic Volume')
plt.ylim(0,6500)
plt.show()
The chart for nighttime shows up alright, but the xtick labels are in [0,5,10,15,20,25], how can I change the labels to fit the hours? Something along the lines like: [0,1,2,3,4,5,6,19,20,21,22,23]
I have tried
x=[0,1,2,3,4,5,6,19,20,21,22,23]
plt.xticks(x)
But then I just got [0-6] on the left, and [19-23] on the right, both crammed on either side, leaving the middle of the xticks blank.
Or is there a better way to plot the chart? Since there will be a breaking point between 6 and 19 hours, is there a way to avoid this?
I am new to python and matplotlib, so forgive me if my wordings aren't precise enough.
xticks takes in two arguments: an array-like object of the placements and an array-like object of the labels. So you can do something like this:
plt.xticks(x, x)
This will set a label equal to the placement of the xtick. For more info you can read the docs for xtick here

Why are my plots being displayed separately, rather than on the same graph?

I have created two line plots with this dataset. The first lineplot shows the number of flight accidents in a given year. The second lineplot shows the number of fatalities in a given year. I want to put both line plots on the same graph. This is the code I have used:
fatalities=df[['Fatalities','Date']]
fatalities['Year of Fatality']=fatalities['Date'].dt.year
fatalities.drop('Date',inplace=True)
fatalities.set_index('Year of Fatality',inplace=True)
fatalities.sort_index(inplace=True)
plt.figure(figsize=(12,9))
plt.title("Number of Flight Accidents Since 1908",fontsize=20)
plt.ylabel("Number of Flight Accidents")
plt.xlabel("Year")
plt.xticks(year.index,rotation=90)
year.plot()
fatalities.plot()
plt.show()
What I get are two plots, with on above the other: the plot which shows the number of fatalities and the plot which shows the number of flight accidents.
What I want is one graph that shows the two line plots. Any help would be much appreciated. (Side note: how can I rotate the xticks 90 degrees? I used the rotation argument in the plt.xticks() but this had zero affect).
Given the use of .plot() and variables called df, I assume you're using pandas dataframes (if that's not the case, the answer still probably applies, look up the docs for your plot function).
Pandas' plot by default puts the plots in their own axis, unless you pass one to draw on via the ax attribute:
fig, ax = plt.subplots()
year.plot(ax=ax)
fatalities.plot(ax=ax)

Setting physically square subplots in Matplotlib 3 with shared axes

I am trying to create a similar plot to what is done by seaborn, but in native matplotlib.
I am plotting every Series in a data frame against every other series in a matrix of plots.
So far I've plotted it, marked the outer axes, and set the axes to be shared along columns and row (as this works with the data the best).
The final step I am failing to manage is to make all the plots physically square in dimension. The following code:
#scatter matrix
def plot_scatter_matrix(data):
dim = len(data.columns.values)
fig, axs = newfigure(dim, dim, sharex='col', sharey='row', figsize=(10,10))
fig.tight_layout()
for row, iname in enumerate(data.columns.values):
for col, jname in enumerate(data.columns.values):
axs[row,col].scatter(data[jname], data[iname])
if col == 0:
axs[row,col].set_ylabel(iname)
if row == len(data.columns.values)-1:
axs[row,col].set_xlabel(jname)
return fig, axs
fig, axs = plot_scatter_matrix(ndata)
plt.show()
achieves this (only top half pictured):
I have attempted to use axs[row,col].set_aspect(1.0, adjustable='box', share=True) after the call to scatter() however it simply resulted in this:
As you can see, some managed to become physically square but they are all different sizes.
Having looked extensively through documentation and other questions I am stumped. Doesn't make it easier when other methods for this sort of thing have been deprecated over past versions.
If some axes become square by using set_aspect(1.0) (or the equivalent set_aspect("equal")) that's more or less coincidence and would only happen when the diffence of axis limits is actually equal; e.g. when the data ranges for x and y are the same.
Of course you could share all axes, not just column- or row-wise. That would ensure all axes to be of equal shape - but not necessarily square.
The requirement for square axes is that the aspect is the quotient of the x- and y range.
ax.set_aspect(np.diff(ax.get_xlim())/np.diff(ax.get_ylim()))
Also see: How to make sure that both x and y axes of plot are of equal sizes?
Another option is to restrict the space the subplots have via the subplot parameters as shown in this answer to python interplay between axis('square') and set_xlim.

Plot two datasets at same position based on their index

I'm trying to plot two datasets (called Height and Temperature) on different y axes.
Both datasets have the same length.
Both datasets are linked together by a third dataset, RH.
I have tried to use matplotlib to plot the data using twiny() but I am struggling to align both datasets together on the same plot.
Here is the plot I want to align.
The horizontal black line on the figure is defined as the 0°C degree line that was found from Height and was used to test if both datasets, when plotted, would be aligned. They do not. There is a noticable difference between the black line and the 0°C tick from Temperature.
Rather than the two y axes changing independently from each other I would like to plot each index from Height and Temperature at the same y position on the plot.
Here is the code that I used to create the plot:
#Define number of subplots sharing y axis
f, ax1 = plt.subplots()
ax1.minorticks_on()
ax1.grid(which='major',axis='both',c='grey')
#Set axis parameters
ax1.set_ylabel('Height $(km)$')
ax1.set_ylim([np.nanmin(Height), np.nanmax(Height)])
#Plot RH
ax1.plot(RH, Height, label='Original', lw=0.5)
ax1.set_xlabel('RH $(\%)$')
ax2 = ax1.twinx()
ax2.plot(RH, Temperature, label='Original', lw=0.5, c='black')
ax2.set_ylabel('Temperature ($^\circ$C)')
ax2.set_ylim([np.nanmin(Temperature), np.nanmax(Temperature)])
Any help on this would be amazing. Thanks.
Maybe the atmosphere is wrong. :)
It sounds like you are trying to align the two y axes at particular values. Why are you doing this? The relationship of Height vs. Temperature is non-linear, so I think you are setting the stage for a confusing graph. Any particular line you plot can only be interpreted against one vertical axis.
If needed, I think you will be forced to "do some math" on the limits of the y axes. This link may be helpful:
align scales

Matplotlib Interactive subplots with integers and dates on X axes

I am trying to create two interactive plots where the first plot is simply a plot of x and y and the second plot is a subplot which plots dates (fulldate) on its x axis, which correspond to the integer values of x (x axis values) from the first plot.
This code almost does what I want. The only problem is that the dates are not linked to the integers, so when I use the zoom function on the graph, it zooms into the first plot and the subplot is linked and zooms also, but the dates stay stationary and therefore are completely inaccurate.
note that this is just a simplified version of my program. i will be rearranging the dates on the bottom display to got on the bottom.
The integers and the dates must be linked because in my actual program i will be using integers to keep count of the days in the time series.
import matplotlib.pyplot as plt
import seaborn as sns
x=[1,5,7,4,6]
y=[1,3,8,4,6]
fulldate=['01/01/2018','02/01/2018','03/01/2018','04/01/2018','05/01/2018']
with sns.axes_style("darkgrid"):
ax1=plt.subplot2grid((6,1),(0,0),rowspan=3,colspan=1)
ax2=plt.subplot2grid((6,1),(4,0),rowspan=1,colspan=1,sharex=ax1)
ax2v = ax2.twiny()
ax1.plot(x,y)
ax2v.fill_between(fulldate,'Dates')
for label in ax2v.xaxis.get_ticklabels():
label.set_rotation(60)

Categories