Python Plotly Express Scatter Plot - python
I want to create an interactive scatter plot; so I am using the plotly.graph_objects module.
My data has two columns of about 100 points.
When I make a line plot, I have no problem.
But when I try to make a scatter plot, Jupyter seems to hang (message at the bottom says - Local Host not responding)
It takes a while for Jupyter to respond and I still have no plot.
The code I am using is:
import plotly.express as px
import plotly.graph_objects as go
fig = go.Figure()
var_list = ['cloxth1 ()','cloxth2 ()']
for item in var_list:
stripped_item = item.replace(' ()','')
fig.add_trace(go.Scatter(
x=np.linspace(0,len(df),len(df)),
y=df[item],
mode='markers',
marker={'size':1},
name = item
))
fig.update_layout(title = 'CLOXTH',
xaxis_title = 'data samples',
yaxis_title = 'mV')
fig.show()
Is there anything wrong with the way I am using go.Scatter?
I tried using px.scatter instead. It seems to work, as in I get a scatter plot. But in the plotly.express case I am unable to have a proper legend for 'cloxth1' and 'cloxth2'; also, both data sets are plotted with the same color.
How can I get around this?
A few rows from the data:
Sample Data
# read in with
df = pd.read_clipboard(sep=',', index_col=[0])
# copy to clipboard
,time(s),Filename,time_stamp,time_vector(ms),time_vector_zerobased(ms),cloxth1(),cloxth2()
0.0,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:03.8,0,0,725.9097285,725.9097285
1.001,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:04.8,1001,1001,725.9097285,725.9097285
2.001,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:05.8,2001,2001,725.9097285,725.9097285
3.002,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:06.8,3002,3002,725.9097285,725.9097285
4.0,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:07.8,4000,4000,725.9097285,725.9097285
5.002,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:08.8,5002,5002,725.9097285,725.9097285
6.002,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:09.8,6002,6002,725.9097285,725.9097285
7.001,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:10.8,7001,7001,725.9097285,725.9097285
8.003,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:11.8,8003,8003,725.9097285,725.9097285
9.002,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:12.8,9002,9002,725.9097285,725.9097285
10.0,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:13.8,10000,10000,725.9097285,725.9097285
11.005,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:14.8,11005,11005,725.9097285,725.9097285
12.0,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:15.8,12000,12000,725.9097285,725.9097285
13.001,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:16.8,13001,13001,725.9097285,725.9097285
14.003,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:17.8,14003,14003,725.9097285,725.9097285
15.0,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:18.8,15000,15000,725.9097285,725.9097285
16.002,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:19.8,16002,16002,725.9097285,725.9097285
17.0,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:20.8,17000,17000,725.9097285,725.9097285
18.0,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:21.8,18000,18000,725.9097285,725.9097285
19.003,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:22.8,19003,19003,725.9097285,725.9097285
20.001,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:23.8,20001,20001,725.9097285,725.9097285
21.0,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:24.8,21000,21000,725.9097285,725.9097285
22.005,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:25.8,22005,22005,725.9097285,725.9097285
23.0,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:26.8,23000,23000,725.9097285,725.9097285
24.002,4DRBUP1N8HB706662_Trip-Detail_2020-07-20,00-04-03.csv.zip,04:27.8,24002,24002,725.9097285,725.9097285
Related
Adding a market to a line chart Plotly python
I'm trying to add a point to the last observation on a time series chart with plotly. It is not very different from the example here https://stackoverflow.com/a/72539011/3021252 for instance. Except it is the last observation. Unfortunately following such pattern modifies the axis range. Here is an example of an original chart import plotly.express as px df = px.data.gapminder().query("country=='Canada'") fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada') fig.show() But after adding a marker import plotly.graph_objects as go fig.add_trace( go.Scatter( x=[df["year"].values[-1]], y=[df["lifeExp"].values[-1]], mode='markers' ) ) It looks like that Has anyone have an idea how not to introduce this gap on the right?
How to add a secondary Y axis to a Plotly Express bar plot?
I would like to add a second Y axis to my bar plot bellow, that is the number of citizens in integer: this graph was made using plotly: import plotly.express as px fig = px.bar(df, x="country",y="pourcent_visit",color="city",barmode='group') # fig.add_hline(y=10) fig.show()
To my knowledge, there's no direct way to do this. But you can easily build a Plotly Express figure, grab the traces (and data structures) from there and combine them in a figure that allows multiple axes using fig = make_subplots(specs=[[{"secondary_y": True}]]). With no provided data sample, I'll use the built-in dataset px.data.tips() that I'm guessing to a large part resembles the structure of your real world dataset judging by the way you've applied the arguments in px.bar(). Details in the comments, but please don't hesitate to let me know if something is unclear. Plot: Complete code: import plotly.express as px import plotly.graph_objects as go from plotly.subplots import make_subplots # sample data df = px.data.tips() # figure setup with multiple axes fig = make_subplots(specs=[[{"secondary_y": True}]]) # build plotly express plot fig2 = px.bar(df, x="day", y="total_bill", color="smoker", barmode="group") # add traces from plotly express figure to first figure for t in fig2.select_traces(): fig.add_trace(t, secondary_y = False) # handle data for secondary axis df2 = df.groupby('day').agg('sum')#.reset_index() df2 = df2.reindex(index = df['day'].unique()).reset_index() # fig.add_trace(go.Scatter(x = df2['day'], y = df2['size'], mode = 'lines'), secondary_y = True) # fix layout fig.update_layout(legend_title_text = 'smoker') fig.show()
How to Insert numerical information at plotly chart legend
I'm trying to added some data at my chart legends but i don't know how. I did search at plotly docs at https://plotly.com/python/legend/ but none of those examples available bring this feature. In the figure below is showed what i want to do. As you can see there is a legend of my chart and i want insert the data corresponded to the name of legend, i.g: UCL - 100, ICL - 50 and so on. Here is what i have: Here is a real example of what i really aim to: A piece of the code i'm using is below, I can't share the rest: fig.add_trace(go.Scatter( x=df_mean_control_chart['Samples'], y=df_mean_control_chart['UCL'], mode='lines', name='UCL', line=dict(color='black', width=2))) Description of the variables: df_mean_control_chart['Samples'] and df_mean_control_chart['UCL'] = it's a column of a data from a dataframe which only contains numerical data.
You can add numerical values to the legend by using f-string to add the numerical value you wish to add to the legend. import plotly.express as px import plotly.graph_objects as go df = px.data.stocks() goog_max = df['GOOG'].max() goog_mean = df['GOOG'].mean() goog_min = df['GOOG'].min() fig = go.Figure() fig.add_trace(go.Scatter(x=df.index, y=df['GOOG'], name='GOOG')) fig.add_trace(go.Scatter(mode='lines', x=df.index, y=[goog_mean]*len(df), name=f'GOOG {round(goog_mean,2)}')) fig.add_trace(go.Scatter(mode='lines', x=df.index, y=[goog_max]*len(df), name=f'GOOG {round(goog_max,2)}')) fig.add_trace(go.Scatter(mode='lines', x=df.index, y=[goog_min]*len(df), name=f'GOOG {round(goog_min,2)}')) fig.show()
Huge proglem to load Plotly plots in Jupyter Notebook Python
I have a huge problem with Jupyter Notebook. When he builds graphs using the plot packet, to display the next graph I have to reload the dataset, otherwise an error pops up: TypeError: list indices must be integers or slices, not str. When I reopen the project, all the graphs created using Plotly are not visible, just a white background and I have to reload them if I want to see them, and everything after reloading the dataset, that is: 1) I open the project in Jupyte Notebook, all graphs created by Plotly are not visible (others from Seaborn for example are visible. 2) I load the Plotly charts, but I can load them one by one, because first I have to load the dataset, then the chart, and so an error occurs to all Plotly charts differently: TypeError: list indices must be integers or slices what can I do about it how to fix it? it seriously hinders the work this is my libraries: import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns %matplotlib inline import plotly.offline as py import plotly.graph_objs as go import plotly.tools as tls This is an example of this error, but if I load the dataset again and again load this plot everything will be ok: and the example of the code of plot: #Distribution of credit risk in dataset (target variable) #Size of the plot figsize=(10,5) #Sums of good and bad credits in the dataset goodCount = data[data["Risk"]== 'good']["Risk"].value_counts().values badCount = data[data["Risk"]== 'bad']["Risk"].value_counts().values #Bar fo good credit trace0 = go.Bar(x = data[data["Risk"]== 'good']["Risk"].value_counts().index.values, y = data[data["Risk"]== 'good']["Risk"].value_counts().values, name='Good credit', text= goodCount, textposition="auto", marker = dict(color = "green", line=dict(color="black", width=1),),opacity=1) #Bar of bad credit trace1 = go.Bar(x = data[data["Risk"]== 'bad']["Risk"].value_counts().index.values, y = data[data["Risk"]== 'bad']["Risk"].value_counts().values, name='Bad credit', text= badCount, textposition="auto", marker = dict(color = "red", line=dict(color="black", width=1),),opacity=1) #Creation of bar plot data = [trace0, trace1] layout = go.Layout() layout = go.Layout(yaxis=dict(title='Count'), xaxis=dict(title='Risk variable'), title='Distribution of target variable in the dataset') fig = go.Figure(data=data, layout=layout) fig.show()
Plotly: How to make line charts colored by a variable using plotly.graph_objects?
I'm making a line chart below. I want to make the lines colored by a variable Continent. I know it can be done easily using plotly.express Does anyone know how I can do that with plotly.graph_objects? I tried to add color=gapminder['Continent'], but it did not work. Thanks a lot for help in advance. import plotly.express as px gapminder = px.data.gapminder() import plotly.graph_objects as go fig = go.Figure() fig.add_trace(go.Scatter(x=gapminder['year'], y=gapminder['lifeExp'], mode='lines+markers')) fig.show()
Using an approach like color=gapminder['Continent'] normally applies to scatterplots where you define categories to existing points using a third variable. You're trying to make a line plot here. This means that not only will you have a color per continent, but also a line per continent. If that is in fact what you're aiming to do, here's one approach: Plot: Code: import plotly.graph_objects as go import plotly.express as px # get data df_gapminder = px.data.gapminder() # manage data df_gapminder_continent = df_gapminder.groupby(['continent', 'year']).mean().reset_index() df = df_gapminder_continent.pivot(index='year', columns='continent', values = 'lifeExp') df.tail() # plotly setup and traces fig = go.Figure() for col in df.columns: fig.add_trace(go.Scatter(x=df.index, y=df[col].values, name = col, mode = 'lines')) # format and show figure fig.update_layout(height=800, width=1000) fig.show()