I am trying to reproduce a simple example from holoviews. I am running bokeh Version: 0.12.11 and holoviews Version: 1.9.1.
Here is how it goes.
import numpy as np
import pandas as pd
import holoviews as hv
hv.extension('bokeh')
macro_df = pd.read_csv('http://assets.holoviews.org/macro.csv', '\t')
key_dimensions = [('year', 'Year'), ('country', 'Country')]
value_dimensions = [('unem', 'Unemployment'), ('capmob', 'Capital Mobility'),
('gdp', 'GDP Growth'), ('trade', 'Trade')]
macro = hv.Table(macro_df, key_dimensions, value_dimensions)
%%opts Scatter [width=700 height=400 scaling_method='width' scaling_factor=2 size_index=2 show_grid=True]
%%opts Scatter (color=Cycle('Category20') line_color='k')
%%opts NdOverlay [legend_position='left' show_frame=False]
gdp_unem_scatter = macro.to.scatter('Year', ['GDP Growth', 'Unemployment'])
gdp_unem_scatter.overlay('Country')
This is my result:
No matter what I do, I can't color the categories as depicted in the example here. Even if I define my own color...
What am I doing wrong?
Related
For my dataset, I wanted to change the colors of the "Labels" data that is shown in the 3d scatter plot, but I have been unsuccessful.
I keep getting these default colors:
This is the code that I am using:
import numpy as np
import os
import pandas as pd
from matplotlib import pyplot
import matplotlib.pyplot as plt
import plotly.express as px
import seaborn as sns
# Data
data = pd.read_csv('SamplePlotlyData.csv')
labels = data['Label'].values
data = data.drop(columns=['Label']).values
fig = px.scatter_3d(data,
x= data[:,0], y= data[:,1], z = data[:,2],
labels={'x':'PCA-1', 'y':'PCA-2','z':'PCA-3'},
color=labels,
color_discrete_sequence=["blue", "goldenrod", "magenta"],
title='3d Plot of Top 3 PCA components')
fig.show()
Can you assist me in correctly changing the color palette of the 3d scatter plot?
I am using jupyter notebook 6.0.3 with seaborn version 0.11.2
Here is my dataset:
36 37 38 39 Label
0.22717583 -0.1028256 -0.041157354 0.047657568 0
-1.242205 2.611936 1.5563084 -0.64137465 0
0.39261582 0.40208274 0.2835228 0.26541463 0
-4.296567 -1.3980201 -0.67690927 -0.941123 0
-1.5278594 1.103121 -1.4688232 -1.139884 0
2.35497 -1.3783572 0.4808609 -1.4851115 1
-0.055658106 -0.19007513 -0.40134305 -0.34722504 1
0.051404 -0.6016376 0.26404122 -0.42829922 1
-0.47935575 -0.049984064 0.67335206 0.123305336 1
0.57357675 0.9523434 -0.05714764 -0.6305638 1
0.1044371 1.2541072 0.1957058 0.083972946 2
0.47575372 0.18598396 0.069036044 0.63252586 2
-0.7613742 0.81920165 0.43508404 0.280004 2
-0.16776349 0.9296196 -1.1710609 0.86310846 2
-0.20844702 0.3536006 0.01729327 -0.28363776 2
One of the reasons you are seeing the colors is because your Label column is integer. Seaborn thinks it is numerical and uses continuous colors. So, you will need to change that to categorical using .astype(str). Also, I think you are moving Label to a labels and deleting the column, which is not required. So, I have updated it as below. Also attached the output plot.
import numpy as np
import os
import pandas as pd
from matplotlib import pyplot
import matplotlib.pyplot as plt
import plotly.express as px
import seaborn as sns
# Data
data = pd.read_csv('SamplePlotlyData.csv')
data['Label'] = data['Label'].astype(str) #Make it string
#data = data.drop(columns=['Label']).values
fig = px.scatter_3d(data,
x= 36, y= 37, z = 38, #Updated here - just use column names
labels={'x':'PCA-1', 'y':'PCA-2','z':'PCA-3'},
color='Label', #Column name of Label
color_discrete_sequence=["blue", "goldenrod", "magenta"],
title='3d Plot of Top 3 PCA components')
fig.show()
Output
I'm trying to render a Plotly graph in PyCharm's SciView-Plots panel (using PyCharm Professional 2020.3).
A simplified version of what I'm running is:
import plotly.express as px
import plotly.io as pio
pio.renderers.default = 'png'
fig = px.scatter(data)
fig.show()
This code runs, but does not show a plot. Instead I see a printout in the console that looks like:
{'image/png': 'iVBORw0KGgoAAAANSUh<cropped for brevity...>'}
I was working on following this article, but the solutions there don't seem to work: how can I see plotly graphs in pycharm?
Sciview will support matplotlib.pyplot.show, therefore you can export to png, then display the png using matplotlib.pyplot.show
import io
import numpy as np
import plotly.express as px
import plotly.io as pio
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
pio.renderers.default = 'png'
def plot(fig):
img_bytes = fig.to_image(format="png")
fp = io.BytesIO(img_bytes)
with fp:
i = mpimg.imread(fp, format='png')
plt.axis('off')
plt.imshow(i, interpolation='nearest')
plt.show()
x = np.arange(0, 5, 0.1)
y = np.sin(x)
fig = px.scatter(x, y)
plot(fig)
works with Pycharm professional 2019.3
I am not able to change default color from blue to red using plt.rc() or mpl.rcparams
According to Matplotlib's official documentation
Here is the code I tried:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
data = np.random.randn(50)
mpl.rcParams['lines.color'] = 'r'
plt.plot(data)
Output:
If you are using a relatively new matplotlib version (i.e. 1.5+) you should use axes.prop_cycleas described here.
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
data = np.random.randn(50)
import cycler
plt.rcParams['axes.prop_cycle'] = cycler.cycler(color='r')
plt.plot(data)
I have created many graphs using plotly library in jupyter notebook. I saved the file and exit. My graph is not appearing when I re-opened the jupyter notebook again? I need to run the code again in order to get my graph back. Also, I have sent my saved ipynb file to my friend and he is unable to see the plot..Any suggestions?
I used py.offline.init_notebook_mode(connected=True)
import plotly.plotly as py
import cufflinks as cf
import plotly as py
import plotly.graph_objs as go
import ipywidgets as widgets
import numpy as np
from scipy import special
import datetime
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.figure_factory as ff
init_notebook_mode
from math import floor
from plotly import tools
from plotly.graph_objs import *
from IPython.display import display
import plotly.tools as tls
import json
import math
from plotly.widgets import GraphWidget
import plotly.io as pi
data = [go.Bar(
x = dplot['Boxoffice_US_Mil'],
y = dplot['dircount'],
orientation = 'h',
marker = dict(
color = 'rgb(67,262,402)'))]
# Declaring the Title and margins
layout = dict(
title = 'Average Box Office collections by Director who have directed more than 3 movies',
margin = go.Margin(
l = 210,
r = 100,
pad = 1),
xaxis = dict(
title = 'Average Box Office Collection (US mil)'),
yaxis = dict(
title = ' Director (Number of Movies)',
tickfont = dict(
size = 12,)))
fig = go.Figure(data = data, layout = layout)
iplot(fig)
Is it possible to remove the Bokeh logo from plots generated with HoloViews? Nothing against it... it's just that it may not make sense to display it in certain reports. :)
I know that in Bokeh I can simply do:
p = bkp.figure(...)
...
p.toolbar.logo = None
UPDATE
Here's my import section:
import sys
import os
import numpy as np
np.random.seed(0)
import random
random.seed(0)
import pandas as pd
from bokeh.models import HoverTool
import holoviews as hv
hv.extension("bokeh", logo=False)
Currently (as of holoviews 1.9.1) the option to disable the bokeh logo in the toolbar is not directly exposed, but you can supply a so called finalize_hook which lets you modify the plot directly. You can add such a hook directly on the ElementPlot to set it globally:
def disable_logo(plot, element):
plot.state.toolbar.logo = None
hv.plotting.bokeh.ElementPlot.finalize_hooks.append(disable_logo)
or set it as a plot option:
hv.Curve(range(10)).opts(plot=dict(finalize_hooks=[disable_logo])
To remove the Bokeh logo for more complicated layouts, I think you need to render it to a Bokeh figure, and then use Bokeh's native method to remove it.
layout = C + D
plot = renderer.get_plot(layout)
p = plot.state
p.children[0].toolbar.logo = None
show(p)
hv.extension("bokeh",logo=False)
1) This is almost the same as philippjfr answer, but slightly shorter using hooks:
def remove_bokeh_logo(plot, element):
plot.state.toolbar.logo = None
hv.Scatter(df).opts(hooks=[remove_bokeh_logo])
2) And there's Andrew's answer, rendering the plot as bokeh and then removing the logo:
from bokeh.plotting import show
hv_plot = hv.Scatter(df)
bokeh_plot = hv.render(hv_plot, backend='bokeh')
bokeh_plot.toolbar.logo = None
show(bokeh_plot)