I am using cufflinks for plotting candlestick charts in jupyter notebook.
How to remove gaps of non trading days in chart.
In plotly we can update layout ['xaxis']['type'] to 'category'.
How can we update the same for a QuantFig in cufflinks.
Follwowing is my code.
import cufflinks as cf
import pandas as pd
import plotly
from plotly import tools
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
print("List of Cufflinks Themes : ", cf.getThemes())
cf.set_config_file(theme='pearl',sharing='public',offline=True)
daily = pd.read_csv('data.csv',index_col=0,parse_dates=True)
qf=cf.QuantFig(daily,title='my test chart',legend='left',name='Test')
qf.add_volume()
qf.iplot()
This did the trick for me:
fig = qf.iplot(kind='candle')
fig.update_xaxes(
rangebreaks=[
dict(bounds=["sat", "mon"]), #hide weekends
]
)
(described here: https://plotly.com/python/time-series/#hiding-weekends-and-holidays)
You may try this
qf=cf.QuantFig(daily,title='my test chart',legend='left',name='Test')
qf.add_volume()
fig = qf.figure()
fig.update_xaxes(
rangebreaks=[dict(bounds=["sat", "mon"])])
fig.show()
Related
How can I hide the color bar from a seaborn generated heatmap
import numpy as np; np.random.seed(0)
import seaborn as sns; sns.set_theme()
uniform_data = np.random.rand(10, 12)
ax = sns.heatmap(uniform_data)
You can use cbar=False for that
ax = sns.heatmap(uniform_data, cbar=False)
I am answering my own question because I only came across the answer in a comment here Matplotlib - How to remove color bar but keep the heatmap position unchanged
More details here https://seaborn.pydata.org/generated/seaborn.heatmap.html
In a Seaborn heatmap (within Jupyter Notebook), I am getting extra space between the axis and the x-ticks, which I've moved to the top. If I leave the ticks at the bottom, they are flush as expected, but I need them at the top. I can't figure how to get rid of that space between the upper edge of the plot and the x-ticks. I tried the padding setting in set_tick_params, but that only adjusts space between the tick and the label.
Here's a subset of the data to play with
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
axis_labels = ['Q1','Q2','Q3','Q4','Q5']
data = pd.DataFrame([[np.nan,0.14,0.01,0.00,-0.05],
[0.30,np.nan,0.01,0.03,-0.04],
[0.16,0.10,np.nan,0.01,-0.02],
[0.14,0.05,0.02,np.nan,-0.04],
[0.16,0.09,0.01,0.02,np.nan]])
fig, ax = plt.subplots(figsize=(15,15))
sb.heatmap(data, ax=ax, center=0, annot=True, mask=data.isnull(),
square=True, cmap=sb.diverging_palette(275, 150, s=80, l=55, as_cmap=True), cbar_kws={"shrink": 0.75})
ax.set_ylim(5,-0.5)
ax.set_xticklabels(axis_labels, rotation=90, ha='center', fontsize=12)
ax.set_yticklabels(axis_labels, rotation=0, fontsize=12)
ax.xaxis.tick_top();
Probably something super simple that I'm missing. Any ideas?
I have a simple factorplot
import seaborn as sns
g = sns.factorplot("name", "miss_ratio", "policy", dodge=.2,
linestyles=["none", "none", "none", "none"], data=df[df["level"] == 2])
The problem is that the x labels all run together, making them unreadable. How do you rotate the text so that the labels are readable?
I had a problem with the answer by #mwaskorn, namely that
g.set_xticklabels(rotation=30)
fails, because this also requires the labels. A bit easier than the answer by #Aman is to just add
plt.xticks(rotation=45)
You can rotate tick labels with the tick_params method on matplotlib Axes objects. To provide a specific example:
ax.tick_params(axis='x', rotation=90)
This is still a matplotlib object. Try this:
# <your code here>
locs, labels = plt.xticks()
plt.setp(labels, rotation=45)
Any seaborn plots suported by facetgrid won't work with (e.g. catplot)
g.set_xticklabels(rotation=30)
however barplot, countplot, etc. will work as they are not supported by facetgrid. Below will work for them.
g.set_xticklabels(g.get_xticklabels(), rotation=30)
Also, in case you have 2 graphs overlayed on top of each other, try set_xticklabels on graph which supports it.
If anyone wonders how to this for clustermap CorrGrids (part of a given seaborn example):
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(context="paper", font="monospace")
# Load the datset of correlations between cortical brain networks
df = sns.load_dataset("brain_networks", header=[0, 1, 2], index_col=0)
corrmat = df.corr()
# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(12, 9))
# Draw the heatmap using seaborn
g=sns.clustermap(corrmat, vmax=.8, square=True)
rotation = 90
for i, ax in enumerate(g.fig.axes): ## getting all axes of the fig object
ax.set_xticklabels(ax.get_xticklabels(), rotation = rotation)
g.fig.show()
You can also use plt.setp as follows:
import matplotlib.pyplot as plt
import seaborn as sns
plot=sns.barplot(data=df, x=" ", y=" ")
plt.setp(plot.get_xticklabels(), rotation=90)
to rotate the labels 90 degrees.
For a seaborn.heatmap, you can rotate these using (based on #Aman's answer)
pandas_frame = pd.DataFrame(data, index=names, columns=names)
heatmap = seaborn.heatmap(pandas_frame)
loc, labels = plt.xticks()
heatmap.set_xticklabels(labels, rotation=45)
heatmap.set_yticklabels(labels[::-1], rotation=45) # reversed order for y
One can do this with matplotlib.pyplot.xticks
import matplotlib.pyplot as plt
plt.xticks(rotation = 'vertical')
# Or use degrees explicitly
degrees = 70 # Adjust according to one's preferences/needs
plt.xticks(rotation=degrees)
Here one can see an example of how it works.
Use ax.tick_params(labelrotation=45). You can apply this to the axes figure from the plot without having to provide labels. This is an alternative to using the FacetGrid if that's not the path you want to take.
If the labels have long names it may be hard to get it right. A solution that worked well for me using catplot was:
import matplotlib.pyplot as plt
fig = plt.gcf()
fig.autofmt_xdate()
I am making scatter plot in seaborn and I want to add some text to each point of scatter plot according to my data ("Countries" column in hap_educ and hap_rel tables). I think I need loop to do this but cannot figure out how to do it for seaborn. Here is code I use:
https://ibb.co/hZ9NBV0
https://ibb.co/ZYLdgkt
import pandas as pd
import os
import seaborn as sns
import matplotlib.pyplot as plt
# Set up working directory
os.chdir(r'D:/PROJECT CSS/')
#importing data from xlsx files
educ = pd.read_excel(r'D:\PROJECT CSS\educ.xlsx')
happiness= pd.read_excel(r'D:\PROJECT CSS\happiness edited.xlsx')
religious=pd.read_excel(r'D:\PROJECT CSS\religious edited.xlsx')
#Merging data into 2 tables
hap_rel = pd.merge(religious, happiness, on ='Country')
hap_educ= pd.merge(educ, happiness, on ='Country')
p1=sns.regplot(x =hap_educ['Score'], y =hap_educ['Pupil teacher ratio'], data=hap_educ, label='Countries')
plt.xlabel("Index of happiness")
plt.ylabel("Pupil / teacher ratio")
p2=sns.regplot(x=hap_rel['Score'], y=hap_rel['Yes'], data=hap_rel)
plt.xlabel("Index of happiness")
plt.ylabel("Percent of religious people(1=100%)")
Expect to see each point to be Annotated with Country name from my table
The basic way to display GeoDataFrame in Altair:
import altair as alt
import geopandas as gpd
alt.renderers.enable('notebook')
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
data = alt.InlineData(values = world[world.continent=='Africa'].__geo_interface__, #geopandas to geojson
# root object type is "FeatureCollection" but we need its features
format = alt.DataFormat(property='features',type='json'))
alt.Chart(data).mark_geoshape(
).encode(
color='properties.pop_est:Q', # GeoDataFrame fields are accessible through a "properties" object
tooltip=['properties.name:N','properties.pop_est:Q']
).properties(
width=500,
height=300
)
But it will crush if I add column with Nan or DateTime values.
At first you can use world = alt.utils.sanitize_dataframe(world) to convert columns with JSON incompatible types.
Or you can use gpdvega module to simplify code.
import altair as alt
import geopandas as gpd
import gpdvega
alt.renderers.enable('notebook')
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
alt.Chart(world[world.continent=='Africa']).mark_geoshape(
).encode(
color='pop_est',
tooltip=['name','pop_est']
).properties(
width=500,
height=300
)
Just pip install gpdvega and import gpdvega. altair will work with GeoDataFrame as usual DataFrame. See details in documentation