pandas dataframe hexbin plot has no xlabel or axis values - python

I am trying to plot a dataframe as hexbin but can't seem to get the xlabel and x-axis values to plot. The dataframe is as follows:
szen_df.xs('left', level='pos')
and the plot is as follows:
szen_df.xs('left', level='pos').plot(x='szen', y='lat', xlim=(0,90), ylim=(-90,90), kind='hexbin', colormap='Reds' )
with result:

I'm seeing this problem as well with python 2.7, pandas 0.16.2.dev, and ipython version 3.1.0.
Looking to the documentation for pandas.DataFrame.plot, the only reference to the x labels and ticks being invisible is in the sharex option. So, I manually set sharex=False, and poof, the xlabel and xticks come back.
Clearly this is a bug, but I hope this helps for the time being. I'll add these comments to the bug report that you've already started.

Looks like the same as the issue I had here: matplotlib scatterplot x axis labels
The workaround which worked was to set the axes explicitly. Just leaving this here for the future :)
Might be connected with https://github.com/pandas-dev/pandas/pull/12949 , Open as of now (6th of April 17)

Related

Error when plotting line graph using seaborn: If using all scalar values, you must pass an index

I am trying to make a more aesthetically pleasing graph for a project and was told that seaborn would make beautiful plots but I am having trouble with it as it returns the error: If using all scalar values, you must pass an index. I'm not sure why there is this error as I am able to plot a regular graph using the same dataframe.
This is the dataframe that I am using:
and I have successfully created a graph:
ax = data1.plot(xlabel='Year', ylabel='Electricity generation capacity', figsize=(15,10), marker='.')
ax.legend(title='Electricity generation capacity by Year', bbox_to_anchor=(1, 1.02), loc='upper left')
However, the graph is quite ugly as you can barely see the trend of the bottom three lines. (I do not know if seaborn will help with this issue as I am rather new to python and am unfamiliar with data visualization using python.)
Perhaps my code is wrong but when I try to make a graph, sns.lineplot(data1) , it returns an error as mentioned above.
Please let me know how I can solve this issue (Or if I can create a better-looking graph without seaborn, please teach me). Thank you.
From your screenshot it seems like the Year is the dataframe index. Try this:
sns.lineplot (data=data1, x=data1.index)

How to adjust subplots borders in matplotlib automatically?

When plotting multiple plots using plt.subplots, most of the time the spacing between subplots is not ideal so the the xtick labels of the top plot would overlap with the title of the bottom plots. There is a way to fix this manually by calling say plt.subplots_adjust(hspace=0.5) and changing the parameters interactively to obtain a decent looking plot. Is there a way to calculate the subplot_adjust parameter automatically? Meaning finding the minimum hspace and wspace so that there is not overlap between texts of the plots.
You can use tight_layout https://matplotlib.org/stable/tutorials/intermediate/tight_layout_guide.html or constrained_layout https://matplotlib.org/stable/tutorials/intermediate/constrainedlayout_guide.html
I'm pretty certain that the closest your going to find to an inbuilt calculation method is:
plt.tight_layout()
or
figure.Figure.tight_layout() #if you are using the object version of the code

Seaborn displot - Can the y axis be set to show density when plot has histogram and KDE

Hi I am new to using Jupyter notebooks, python, matplotlib, pandas & seaborn. I am trying to plot a density plot using the displot function in seaborn. I want to be able to see the histogram aswell but instead of "count" on the y axis I want to see the density.
The old distplot function automatically shows density I just don't know how to set it with the new displot. Any help would be greatly appreciated!
I am going to answer my own question...
stat="density"
example:
sns.displot(data=df_under_100, x="processingtime_wks", stat="density", binwidth=5, binrange=(0.0, 100.0), kde=True)

Python Seaborn FacetGrid different y-axis

I'm using sns.FacetGrid to plot 10 subplots. I'd like to flex the y-axis to be different for each subplot.
At the moment it automatically uses the same for all subplots. Would it be possible to customize it to make it more specific for each subplot?
See the documentation for facet grid here
share{x,y}bool, ‘col’, or ‘row’ optional If True, the facets will
share y axes across columns and/or x axes across rows.
Be advised that this also breaks alignment across columns and will most likely not produce the results you intended. One Y axis will be displayed, which will be only valid for the leftmost plot.

Skip weekends on stock charts with matplolib

This is not duplicate, because existing answers on similar questions don't describe exactly what I need.
Matplotlib has great formatters inside and I love to use them:
ax.xaxis.set_major_locator(matplotlib.dates.MonthLocator())
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%b%y'))
They let me plot such stock market charts:
This is what I need, but it has 1 issue: weekends. They are present on x axis and make my chart a little ugly.
Other questions about this issue give advice to create custom formatter. They show examples of such formatters. But no one of them do pretty formatting like matplotlib do:
May19, Jun19, Jul19...
I mean this line of code:
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%b%y'))
My question is: please help me to format x axis like matplotlib do: May19, Jun19, Jul19... and don't create weekends when stock market is closed.
What you could almost always do is something similar to what Nic Wanavit suggested.
Manually set your labels, depending on what you need on your axis.
Especially in this case the plot is looking a bit ugly because you have timespans in your data that are not provided with actual data (the weekends in this case) so pyplot will simply connect these points with the corresponding length from the x-axis.
What you can do then is just to plot your data equally distant - which is correct if the data is daily - otherwise consider to interpolate it using e.g. pandas bultin interpolation.
To avoid pyplot automatically detect the index I had to do this:
df['plotidx'] = [i for i in range(len(df['close'])):
Here all the closing values for the stock are stored in a column named 'close' obvsl.
You plot this correspondingly.
Then you can obtain all the ticks created via
labels = [item.get_text() for item in ax.get_xticklabels()]
Adjust them as desired with
labels[i] = string_for_the_label_no_i
Then get them back on the graph using
ax.xaxis.set_ticklabels(labels)
You need to somewhat "update" the plot then. Also keep in mind, that resizing a lot could end up with the labels being as also said in the documentation strange location.
It is some kind of a workaround but worked fine for me because it feels natural to plot data equally distant next to each other rather then making up some data for the weekends.
Greets
to set the x ticks
assuming that you have the dates variable in dataframe row df['dates']
ax.xaxis.set_ticks(df['dates'])

Categories